获取jQuery .filter()结果的反面/反面 [英] Get opposite/inverse of jQuery .filter() results

查看:196
本文介绍了获取jQuery .filter()结果的反面/反面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 .filter 函数,它可以工作并标记< td> 输入字段为红色。

但是,我需要的是完全相反的。因此,返回所有不包含< input 的字段。
我试过 if($(this).html()。indexOf('< input')== -1){

我的jQuery:

  $(document).ready (function(){
$(#ListView1_itemPlaceholderContainer table table table td)。filter(function(index){
if($(this).html()。indexOf('< input' )> 0){
return true;
}
})。closest('div')。css('background-color','red');
} );

这是我的HTML

 < div id =ListView1_itemPlaceholderContainerstyle = 
font-family:Verdana,Arial,Helvetica,sans-serif;>
< div style =background-color:#FFFFFF; color:#333333;>
< table style =width:100%;>
< tbody>
< tr>
< td style =width:71px>< img id =ListView1_userInfo1_0_ImageUser_0src =
http://pbs.twimg.com/profile_images/13/6GA5kEu6_normal.pngstyle =
height:65px; width:65px; />< / TD>

< td style =vertical-align:top>
< table style =width:100%;>
< tbody>
< tr>
< td>< / td>
< / tr>

< tr>
< td>
< table style =width:100%;>
< tbody>
< tr>
< td>< img id =ListView1_userInfo1_0_Imagebg_0src =
https://pbs.twimg.com/profile_banners/13/1402427948style =
height:64px;宽度:251px; />< br />
< span id =ListView1_userInfo1_0_LabelName_0> dfgdfg< / span>
& nbsp; @< span id =
ListView1_userInfo1_0_LabelUserName_0> dfgdfg< / span>< / td>

< td style =text-align:right>< / td>
< / tr>
< / tbody>
< / table>
< / td>
< / tr>

< tr>
< td>< span style =font-size:10px> Follower-< span id =
ListView1_userInfo1_0_LabelFollower_0> 4578< / span> &安培; NBSP; ,
以下 - < span id =
ListView1_userInfo1_0_LabelFollowing_0> 1654< / span>< / span>< / td>
< / tr>

< tr>
< td>< span id =ListView1_userInfo1_0_LabelAbout_0>< / span>< / td>
< / tr>
< / tbody>
< / table>
< / td>
< / tr>
< / tbody>
< / table>< br />
< / div>

< div style =background-color:#EEEEEE; color:#333333;>
< table style =width:100%;>
< tbody>
< tr>
< td style =width:71px>< img id =ListView1_userInfo_1_ImageUser_1src =
http://pbs.twimg.com/profile_images/12/ECWtACTn_normal.jpegstyle =
height:65px; width:65px; />< / TD>

< td style =vertical-align:top>
< table style =width:100%;>
< tbody>
< tr>
< td>< / td>
< / tr>

< tr>
< td>
< table style =width:100%;>
< tbody>
< tr>
< td>< img id =ListView1_userInfo_1_Imagebg_1src =
http://pbs.twimg.com/profile_background_images/37/pTBlAXSm.jpeg
style =height: 64PX;宽度:251px; />< br />
< span id =ListView1_userInfo_1_LabelName_1> dfgdfgd< / span>
& nbsp; @< span id =
ListView1_userInfo_1_LabelUserName_1> dfgdfgdf< / span>< / td>

< td style =text-align:right>< input type =submitname =
ListView1 $ ctrl1 $ userInfo $ ButtonFollowvalue =Follow id =
ListView1_userInfo_1_ButtonFollow_1style =
height:100px; width:100px; />< / TD>
< / tr>
< / tbody>
< / table>
< / td>
< / tr>

< tr>
< td>< span style =font-size:10px> Follower-< span id =
ListView1_userInfo_1_LabelFollower_1> 4622< / span> &安培; NBSP; ,
以下 - < span id =
ListView1_userInfo_1_LabelFollowing_1> 4007< / span>< / span>< / td>
< / tr>

< tr>
< td>< span id =ListView1_userInfo_1_LabelAbout_1>< / span>< / td>
< / tr>
< / tbody>
< / table>
< / td>
< / tr>
< / tbody>
< / table>< br />
< / div>
< / div>

我也看看这个帖子


$ b jQuery:使用filter(),但是使用两个结果
。 $ b

试过这个:

 < script type =text / javascript> 
$ b $(document).ready(function(){
$ .fn.invert = function(){
return this.end()。not(this); $ ()($(this).html()。indexOf('b $ b};

$(#ListView1_itemPlaceholderContainer table table table td < input')> 0){
return true;
}
})。invert()。closest('div')。css('background-color','red ');
});

< / script>

但是,这也会使所有的行都变成红色。

.not()以及

一个href =http://api.jquery.com/has-selector/ =noreferrer>:has()

  $(document:.ready(function(){
$(#ListView1_itemPlaceholderContainer table table table td)。 ).css('background-color','red');
});


I have a.filter function, which works and marks all parents of <td> that contain an input field in red.

However, I need the exact opposite. So return all fields that don't contain <input. I tried if ($(this).html().indexOf('<input') == -1) { but that just marks all rows somehow.

My jQuery:

$(document).ready(function () {
        $("#ListView1_itemPlaceholderContainer table table table td").filter(function (index) {
            if ($(this).html().indexOf('<input') > 0) {
                return true;
            }
        }).closest('div').css('background-color', 'red');
});

Here's my HTML

 <div id="ListView1_itemPlaceholderContainer" style=
  "font-family: Verdana, Arial, Helvetica, sans-serif;">
    <div style="background-color: #FFFFFF; color: #333333;">
      <table style="width: 100%;">
        <tbody>
          <tr>
            <td style="width: 71px"><img id="ListView1_userInfo1_0_ImageUser_0" src=
            "http://pbs.twimg.com/profile_images/13/6GA5kEu6_normal.png" style=
            "height:65px;width:65px;" /></td>

            <td style="vertical-align: top">
              <table style="width: 100%;">
                <tbody>
                  <tr>
                    <td></td>
                  </tr>

                  <tr>
                    <td>
                      <table style="width: 100%;">
                        <tbody>
                          <tr>
                            <td><img id="ListView1_userInfo1_0_Imagebg_0" src=
                            "https://pbs.twimg.com/profile_banners/13/1402427948" style=
                            "height:64px;width:251px;" /><br />
                            <span id="ListView1_userInfo1_0_LabelName_0">dfgdfg</span>
                            &nbsp; @<span id=
                            "ListView1_userInfo1_0_LabelUserName_0">dfgdfg</span></td>

                            <td style="text-align: right"></td>
                          </tr>
                        </tbody>
                      </table>
                    </td>
                  </tr>

                  <tr>
                    <td><span style="font-size:10px">Follower-<span id=
                    "ListView1_userInfo1_0_LabelFollower_0">4578</span> &nbsp; ,
                    Following-<span id=
                    "ListView1_userInfo1_0_LabelFollowing_0">1654</span></span></td>
                  </tr>

                  <tr>
                    <td><span id="ListView1_userInfo1_0_LabelAbout_0"></span></td>
                  </tr>
                </tbody>
              </table>
            </td>
          </tr>
        </tbody>
      </table><br />
    </div>

    <div style="background-color: #EEEEEE;color: #333333;">
      <table style="width: 100%;">
        <tbody>
          <tr>
            <td style="width: 71px"><img id="ListView1_userInfo_1_ImageUser_1" src=
            "http://pbs.twimg.com/profile_images/12/ECWtACTn_normal.jpeg" style=
            "height:65px;width:65px;" /></td>

            <td style="vertical-align: top">
              <table style="width: 100%;">
                <tbody>
                  <tr>
                    <td></td>
                  </tr>

                  <tr>
                    <td>
                      <table style="width: 100%;">
                        <tbody>
                          <tr>
                            <td><img id="ListView1_userInfo_1_Imagebg_1" src=
                            "http://pbs.twimg.com/profile_background_images/37/pTBlAXSm.jpeg"
                            style="height:64px;width:251px;" /><br />
                            <span id="ListView1_userInfo_1_LabelName_1">dfgdfgd</span>
                            &nbsp; @<span id=
                            "ListView1_userInfo_1_LabelUserName_1">dfgdfgdf</span></td>

                            <td style="text-align: right"><input type="submit" name=
                            "ListView1$ctrl1$userInfo$ButtonFollow" value="Follow" id=
                            "ListView1_userInfo_1_ButtonFollow_1" style=
                            "height:100px;width:100px;" /></td>
                          </tr>
                        </tbody>
                      </table>
                    </td>
                  </tr>

                  <tr>
                    <td><span style="font-size:10px">Follower-<span id=
                    "ListView1_userInfo_1_LabelFollower_1">4622</span> &nbsp; ,
                    Following-<span id=
                    "ListView1_userInfo_1_LabelFollowing_1">4007</span></span></td>
                  </tr>

                  <tr>
                    <td><span id="ListView1_userInfo_1_LabelAbout_1"></span></td>
                  </tr>
                </tbody>
              </table>
            </td>
          </tr>
        </tbody>
      </table><br />
    </div>
  </div>

I also check out this post jQuery: use filter(), but work with both results as suggested by commenter.

And tried this:

<script type="text/javascript">

    $(document).ready(function () {
        $.fn.invert = function () {
            return this.end().not(this);
        };

        $("#ListView1_itemPlaceholderContainer table table table td").filter(function (index) {
            if ($(this).html().indexOf('<input') > 0) {
                return true;
            }
        }).invert().closest('div').css('background-color', 'red');
    });

</script>

But that also makes all rows red.

解决方案

You can use .not() along with :has()

$(document).ready(function () {
        $("#ListView1_itemPlaceholderContainer table table table td").not(':has(input)').closest('div').css('background-color', 'red');
});

这篇关于获取jQuery .filter()结果的反面/反面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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