Javascript for循环检查单击了哪个div
我有一个这样的函数Javascript for循环检查单击了哪个div,javascript,jquery,for-loop,Javascript,Jquery,For Loop,我有一个这样的函数 $(document).mouseup(function (e) { var container = $("#id1"); if (!container.is(e.target) // if the target of the click isn't the container... && container.has(e.target).length === 0) // ... nor a descendant of t
$(document).mouseup(function (e)
{
var container = $("#id1");
if (!container.is(e.target) // if the target of the click isn't the container...
&& container.has(e.target).length === 0) // ... nor a descendant of the container
{
container.fadeOut();
}
var container2 = $("#id2");
if (!container2.is(e.target) // if the target of the click isn't the container...
&& container2.has(e.target).length === 0) // ... nor a descendant of the container
{
container2.fadeOut();
}
}
它基本上会检查单击是否发生在div之外,如果是,div就会消失。然而,我有15-20个时间来检查,想知道是否有比复制粘贴更简短的解决方案
谢谢 为每个div指定相同的类,然后尝试以下操作:
$(".yourClass").each(function() {
if (!this.is(e.target) // if the target of the click isn't the container...
&& this.has(e.target).length === 0) // ... nor a descendant of the container
{
this.fadeOut();
}
});
$(这个).fadeOut()有什么问题代码>?您可以使用选择器将其限制为所需的div。但我必须检查所有ID。我在寻找像var id=['#id1','#id2','#id3'…]这样的函数,以及一个检查其中是否有一个是单击的div,如果没有完全消失,则检查这个div是否消失的函数。您是否考虑过给每个div一个相同的类并使用进行迭代。当您给所有div一个类时,每个
?仍然可以在一行中:$('.clickableDiv')。on('click',function(){$('.clickableDiv')。not($(this)).fadeOut();});