Bootstrap Table中data-header-style属性的classes配置失效问题求助
Bootstrap Table data-header-style 中 classes 不生效的解决方案
我之前也踩过这个坑!你遇到的问题其实是Bootstrap Table headerStyle函数的配置项名称搞错了——不是classes,而是class(单数形式)。
你看,你写的返回对象里用了classes: 'my-header',但Bootstrap Table的API规定这里应该用class键来指定自定义类名。而css属性能正常工作,是因为它的键名是正确的,这就解释了为什么字体样式生效但背景色没反应。
修复后的完整代码如下:
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-table@1.27.1/dist/bootstrap-table.min.css"> <script src="https://cdn.jsdelivr.net/npm/bootstrap-table@1.27.1/dist/bootstrap-table.min.js"></script> <table id="table" data-toggle="table" data-height="460" data-header-style="headerStyle" data-url="json/data1.json" > <thead> <tr> <th data-field="id"> Item ID </th> <th data-field="name"> Item Name </th> <th data-field="price"> Item Price </th> </tr> </thead> </table> <script> function headerStyle(column) { return { css: { 'font-weight': 'normal', color : 'blue' }, // 关键修改:把classes改为class(单数) class: 'my-header' } } </script> <style> .my-header { background-color: aqua; /* 可选:添加!important提升样式优先级,避免被Bootstrap默认表头样式覆盖 */ background-color: aqua !important; } </style>
另外补充一点:如果加上class后样式还是不生效,大概率是Bootstrap默认的表头样式优先级更高,这时候给.my-header的背景色加个!important就能解决这个问题。
内容的提问来源于stack exchange,提问作者albeiroval
相关产品推荐
相关产品推荐

