繁体   English   中英

"JS - jQuery 删除所有包含一个类的带区,但保留带有 URL 中 ID 的带区"

[英]JS - jQuery Remove all bands that contains a class but keep the one with the ID that is in the URL

我需要你的帮助。 我是 Javascript 和 jQuery 的新手。 我有事情要做。 我有一个具有相同类的 div 列表,但每个 div 都有一个特定的 ID。 假设我有这个:

<div class="list" id="apples"></div>
<div class="list" id="bananas"></div>
<div class="list" id="pineapples"></div>

使用filter(function)并检查函数中的 id

$("div.list").filter(function(){
   return !window.location.href.contains(this.id)
}).remove();

这将仅删除 id 值不在 href 中的那些

我试过了,效果很好,但我仍然有 Uncaught TypeError: window.location.href.contains is not a function

$("div.list").filter(function(){
   if (window.location.href.indexOf(this.id) > -1)
    {
        return !window.location.href.contains(this.id)
    }
    $("div.list").remove();
});

我该怎么做才能消除错误? 如果我用“indexOf”替换“包含”,那么我没有任何结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM