![](/img/trans.png)
[英]If URL contains this string then hide all divs with this class/id (loop script)
[英]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.