【问题标题】:Filter div based in its id根据 id 过滤 div
【发布时间】:2013-01-28 18:33:36
【问题描述】:

我可以使用以下代码成功隐藏 id 中包含字符串的 div:

按钮:

<input type="button" id="mytest" value="filter"></input>

js代码:

//hides divs with class=screen3 and with 99 in its id
$('#myteste').click (function () {
    $('div.screen3[id*="99"]').hide();
});

现在我需要做相反的事情,隐藏 id 中不包含字符串但我不知道如何操作的 div。

【问题讨论】:

  • 数学你好!我正在尝试做类似的事情(在搜索时根据他们的 id 隐藏 div),你介意分享你的代码吗?
  • @John 我相信这是您正在寻找的代码:stackoverflow.com/questions/14568750/… 有帮助吗?

标签: jquery filter


【解决方案1】:

你可以这样做:

$('div.screen3').not('[id~="99"]').hide(); // tests the word (delimited by spaces)

$('div.screen3').not('[id*="99"]').hide(); // tests the string

【讨论】:

  • $('div.screen3').not('[id*="99"]').hide(); 工作得很好,谢谢
【解决方案2】:

尝试使用 :not

$('#myteste').click (function () {
    $('div.screen3:not([id*="99"])').hide();
});

【讨论】:

  • +1 这比!= 更通用,我相信它也更具可读性。
猜你喜欢
  • 2013-02-01
  • 2020-05-22
  • 1970-01-01
  • 1970-01-01
  • 2019-04-29
  • 2019-06-08
  • 2021-09-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多