You need to enable JavaScript to run this app. Bootstrap 5 tooltip如何从title传入class、style等指定标签属性
优惠活动
大模型
产品
解决方案
定价
更多

Bootstrap 5 tooltip如何从title传入class、style等指定标签属性

解决方案

你遇到的问题是两个配置项没配全导致的:

  • 第一,Bootstrap Tooltip默认不解析title内的HTML内容,需要手动开启html: true参数
  • 第二,你目前仅配置了允许表格相关标签存在,没有放开对应标签的class、style属性权限,这些属性会被默认过滤掉

完整可用配置代码

// 先修改allowList配置,该操作必须放在tooltip初始化之前执行
var myDefaultAllowList = bootstrap.Tooltip.Default.allowList;
// 注册允许的表格标签
myDefaultAllowList.table = [];
myDefaultAllowList.tr = [];
myDefaultAllowList.thead = [];
myDefaultAllowList.tbody = [];
// 给th、td标签放开style、class属性使用权限
myDefaultAllowList.th = ['style', 'class'];
myDefaultAllowList.td = ['style', 'class'];

$(document).ready(function(){
  // 初始化tooltip时开启HTML解析
  $('[data-toggle="tooltip"]').tooltip({
    html: true
  });
});

效果验证

配置完成后,你直接在tooltip的title属性里给表头<th>添加内联样式即可生效,示例写法:

<th style="background-color: green; color: #fff;">表头内容</th>

如果需要临时放开某标签的所有属性权限做测试,可以直接给对应标签的允许属性数组配置通配符,不过不推荐生产环境使用,存在XSS风险:

myDefaultAllowList.th = ['*']

内容的提问来源于stack exchange,提问作者imi36

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.16 15:54:06