复杂的jQuery过滤器()不工作 [英] Complex jQuery filter() not working
问题描述
filter()
选择器,虽然我没有得到任何错误,但它没有对浏览器输出做任何改变。当我使用变量 alert()
时,它表示 Object object
。我明白,问题是在我定义过滤器本身的行,但我无法找到一个类似的例子,我不知道如何解决它。<这里是代码:
HTML
<表>
< tr class =head>< td colspan =2> section title 1< / td>< / tr>
< tr>< td>文字< / td>
< td>< img />< / td>
< / tr>
< tr>< td>文字< / td>
< td>< img />< / td>
< / tr>
< tr class =head>< td colspan =2>部分标题2< / td>< / tr>
< tr>< td>文字< / td>
< td>< img />< / td>
< / tr>
< tr>< td>文字< / td>
< td>< img />< / td>
< / tr>
<! - 你明白了 - >
JavaScript 这个脚本试图用 这是因为你比较了两个不同的对象,总是 但是,我建议使用 I am trying to dynamically insert links to return to the top of the document at the end of every section of a web page (sad to say, but it's table-based layout). I'm using the jQuery Here's the code: HTML JavaScript The script attempts to select each row that precedes a row with That's because you are comparing 2 different objects that is always However, I'd suggest using
这篇关于复杂的jQuery过滤器()不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
< pre $ $(document).ready(function(){
var lastRow = $('tr')。filter(function(){
return $(this ).next()== $(。head); //这就是问题IMO
});
var a ='< tr class =toTop>< td class =topstyle =text-align:rightcolspan =2>< a href =#main> go& uarr;< / a>< / td>< ; / tr>';
lastRow.after(a);
});
class =头部
,然后插入一个带有顶部
链接的行。
false
,你应该使用 is
方法或长度
属性:
var lastRow = $('tr') .filter(function(){
return $(this).next(。head)。length;
// return $(this).next()。is(。head) ;
});
.prev()$ c
$ $ $ $''$'$''。prev(); $ //选择以前的兄弟tr元素
filter()
selector, and while I get no error, it's not making any changes in the browser output. When I use alert()
with the variable, it says Object object
. I understand that the problem is in the line where I define the filter itself, but I was unable to find a similar example, and I don't know how to fix it.<table>
<tr class="head"><td colspan="2">section title 1 </td></tr>
<tr><td>text</td>
<td><img /></td>
</tr>
<tr><td>text</td>
<td>< img /></td>
</tr>
<tr class="head"><td colspan="2">section title 2 </td></tr>
<tr><td>text</td>
<td><img /></td>
</tr>
<tr><td>text</td>
<td>< img /></td>
</tr>
<!-- you get the point -->
$(document).ready(function(){
var lastRow = $('tr').filter(function(){
return $(this).next()==$(".head"); // Here's the problem, IMO
});
var a = '<tr class="toTop"><td class="top" style="text-align:right" colspan="2"><a href="#main">go to top ↑</a></td></tr>';
lastRow.after(a);
});
class="head"
and insert a row with a top
link.false
, you should use is
method or length
property:var lastRow = $('tr').filter(function(){
return $(this).next(".head").length;
// return $(this).next().is(".head");
});
.prev()
method:$('tr.head').prev(); // selects the previous sibling tr element