Javascript jQuery addClass表问题
我有一个使用datatables jQuery插件的表 我使用以下css使最后一列适合文本:Javascript jQuery addClass表问题,javascript,jquery,html,css,whitespace,Javascript,Jquery,Html,Css,Whitespace,我有一个使用datatables jQuery插件的表 我使用以下css使最后一列适合文本: #products tr td:last-child { width: 1%; white-space: nowrap; } 这很好用。然而,我试图在最后两列中这样做,为了跨浏览器,我试图复制jQuery中的功能 我有以下资料: .fitTableLinks{ width:1%; white-space:nowrap; } jQuery是: $("#products tr td:la
#products tr td:last-child {
width: 1%;
white-space: nowrap;
}
这很好用。然而,我试图在最后两列中这样做,为了跨浏览器,我试图复制jQuery中的功能
我有以下资料:
.fitTableLinks{
width:1%;
white-space:nowrap;
}
jQuery是:
$("#products tr td:last-child").addClass("fitTableLinks");
但是这不起作用,但是没有任何控制台错误
谢谢你的帮助 我同意CSS可能是Salman A上面提到的最好的处理方法。如果您想使用jQuery,我认为您必须为每个tr运行代码
$("#products tr").each(function() {
var $this = $(this);
$this.find('td:last-child').addClass("fitTableLinks");
});
如果希望最后两列使用它,只需检查$this.children()的长度
你在等DOM准备好吗?
#products tr td:n最后一个孩子(-n+2)
工作吗?@Karl AndréGagnon,是的,正在进行文档准备呼叫
$("#products tr").each(function() {
var $this = $(this);
var secondToLast = $this.children().length - 2;
$this.find('td:last-child').addClass("fitTableLinks");
$this.find('td').eq(secondToLast).addClass("fitTableLinks");
});