[英]How to add a class to td tag in bootstrap table using jquery
I have used below jquery code for my bootstrap table 我在我的引导表下面使用了jQuery代码
$(document).ready(function() {
$('.table>tr>td').addClass('redBg');
console.log('hg');
});
As well as i tried this one 以及我尝试过的
$(document).ready(function() {
$('.table>tbody>tr>td').addClass('redBg');
console.log('hg');
});
But the class will not added to the td
tag, 但是该类不会添加到td
标签中,
Here is the working fiddle https://jsfiddle.net/udarazz/0vx7tjfq/ 这是工作的小提琴https://jsfiddle.net/udarazz/0vx7tjfq/
Can anyone help me to fix this issue? 谁能帮我解决此问题?
If you omit it, many browsers will implicitly add a <tbody>
container to wrap all table rows. 如果省略它,许多浏览器将隐式添加<tbody>
容器来包装所有表行。 You can verify it by using your browser's developer tools/inspector. 您可以使用浏览器的开发人员工具/检查器进行验证。 The resulting structure: 结果结构:
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
It means <tr>
is no longer a direct child of <table>
, and your table > tr
selector won't work anymore. 这意味着<tr>
不再是<table>
的直接子代,并且table > tr
选择器不再起作用。
To work around it, you can either: 要变通解决此问题,您可以:
<tbody>
in your markup and use table > tbody > tr > td
selector. 在标记中明确包含<tbody>
并使用table > tbody > tr > td
选择器。 table tr > td
不要使用直接子选择器: table tr > td
Working JSFiddle . 工作中的JSFiddle 。
As Bootstrap styles the backgrounds of even rows and hovers, you might need to make your class definition more specific than Bootstrap's built-in styles: 当Bootstrap为偶数行和悬停的背景设置样式时,您可能需要使类定义比Bootstrap的内置样式更具体:
table.table tr > td.redBg {
background-color: red;
}
Alternatively, you can add !important
to your CSS to override: 另外,您可以在CSS中添加!important
来覆盖:
.redBg {
background-color: red !important;
}
UPDATED JQUERY 更新的JQUERY
$(document).ready(function() {
$('.table tr > td').addClass('redBg');
console.log('hg');
});
If its the first <td>
in the <table>
you're trying to add the class to use: 如果它是<table>
的第一个<td>
,则您要添加要使用的类:
$(document).ready(function() {
$('.table tr td:first-child').addClass('redBg');
console.log('hg');
});
https://jsfiddle.net/0vx7tjfq/5/ https://jsfiddle.net/0vx7tjfq/5/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.