复杂的jQuery过滤器()不工作 [英] Complex jQuery filter() not working

查看:148
本文介绍了复杂的jQuery过滤器()不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图动态插入链接返回到网页的每一部分结束时的文档的顶部(可悲的是,但它是基于表格的布局)。我正在使用jQuery 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
< 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()
$ $ $ $''$'$''。prev(); $ //选择以前的兄弟tr元素


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 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.

Here's the code:

HTML

<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 -->

JavaScript

$(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 &uarr;</a></td></tr>';
    lastRow.after(a);
});

The script attempts to select each row that precedes a row with class="head" and insert a row with a top link.

解决方案

That's because you are comparing 2 different objects that is always false, you should use is method or length property:

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

However, I'd suggest using .prev() method:

$('tr.head').prev(); // selects the previous sibling tr element

这篇关于复杂的jQuery过滤器()不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆