复杂的jQueryfilter()不起作用

我试图动态插入链接以返回到网页每个部分末尾的文档顶部(可悲的是,但它是基于表格的布局)。 我正在使用jQuery filter()选择器,虽然我没有得到任何错误,但它没有对浏览器输出进行任何更改。 当我对变量使用alert()时,它表示Object object 。 我理解问题出在我定义filter本身的行中,但我找不到类似的例子,我不知道如何解决它。

这是代码:

HTML

 

JavaScript的

 $(document).ready(function(){ var lastRow = $('tr').filter(function(){ return $(this).next()==$(".head"); // Here's the problem, IMO }); var a = '
'; lastRow.after(a); });

该脚本尝试选择class="head"的行前面的每一行,并插入一个带有top链接的行。

那是因为您要比较两个始终为false不同对象,您应该使用is method或length属性:

 var lastRow = $('tr').filter(function(){ return $(this).next(".head").length; // return $(this).next().is(".head"); }); 

但是,我建议使用.prev()方法:

 $('tr.head').prev(); // selects the previous sibling tr element 
section title 1
text
text
section title 2
text
text
go to top ↑