Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery addClass表问题_Javascript_Jquery_Html_Css_Whitespace - Fatal编程技术网

Javascript jQuery addClass表问题

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

我有一个使用datatables jQuery插件的表

我使用以下css使最后一列适合文本:

#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");
});