【问题标题】:Get opposite/inverse of jQuery .filter() results获取 jQuery .filter() 结果的相反/倒数
【发布时间】:2014-12-05 04:26:28
【问题描述】:

我有一个.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_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>

我还按照评论者的建议查看了这篇帖子 jQuery: use filter(), but work with both results

并尝试了这个:

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

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

【问题讨论】:

  • 如果你想要相反,只需将true更改为false
  • 查看this answer,它展示了如何添加反转函数。
  • @FelixKling 这似乎不适用于我的 HTML。
  • 啊,我猜你在某个地方也需要return true;。无论如何,问题在于您的“行”实际上是 div,并且每个“行”至少包含一个不包含 inputtd。您必须将选择器范围缩小到特定单元格。
  • @Mottie 谢谢,用那里所做的更改更新了我的答案..但同样:所有行都是红色的。

标签: jquery filter inverse


【解决方案1】:

您可以将.not():has() 一起使用

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

【讨论】:

  • @Flo 试试 $("#ListView1_itemPlaceholderContainer td:not(:has('input'))") .closest('td').css('background-color', 'red') jsfiddle.net/q9othg59 ?
【解决方案2】:

我认为你应该接受 Arun 的回答。问题是 indexOf 返回第一个匹配字符 http://www.w3schools.com/jsref/jsref_indexof.asp 的索引,在您的情况下它将为 0,因此条件 > 0 将不适用。另外对于 == -1 为所有行着色的问题,最好将您的函数应用于 tr 而不是 td

你可以把你的函数改成

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

将带有 input 的行标记为红色,或反转条件以执行相反的操作。或者更好 按照 Arun 的建议使用不同的选择器。

input 将行标记为红色的小提琴http://jsfiddle.net/q9othg59/2/ 并为相反的http://jsfiddle.net/q9othg59/4/提供小提琴

【讨论】:

    猜你喜欢
    • 2021-11-13
    • 2016-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多