这个问题基本上是在我试图回答另一个问题的时候出现的。
问题很简单 - 我的标记如下:
<div class="parent">
Parent
<br/>
<input type="text" value="Element 1"/>
<div>Element 2</div>
<span>Element 3</span>
<input type="button" class="hideParent" value="Hide Children"/>
</div>
以及CSS
到hide
.parent
div
.parent>input,.parent>div,.parent>span {
display: none;
}
现在我要做的是click
div.parent
{我要展示children
div.parent
JS
我$('.parent').on('click',function(){
$(this).children().show();
});
以下div.parent
这一点。
hide
正如您所见,div.parent
内部有一个按钮,基本上是hide
child
的孩子。
我尝试以.toggle()
hiding
.hide()
以各种方式尝试$('.hideParent').on('click',function(){
console.log($(this).closest('.parent').children())
//Logs all the children
$(this).closest('.parent').children().hide();
});
,除了.each()
之外,没有人工作过!为什么这种行为发生在以下3种方法中?有人对此有所了解吗?
我为.css
元素尝试了不同的方法:
使用display:none
$('.hideParent').on('click',function(){
console.log($(this).closest('.parent').children())
$(this).closest('.parent').children().each(function(){
console.log('here');//Comes here 5 times as there are 5 elements
$(this).css('display','none !important');
})
});
使用.each()
和.hide()
更改$('.hideParent').on('click',function(){
console.log($(this).closest('.parent').children())
$(this).closest('.parent').children().each(function(){
console.log('here');//Again I can see this
$(this).hide();
})
});
.toggle()
使用$.each()
和$('.parent').on('click',function(){
$(this).children().toggle();
});
$('.hideParent').on('click',function(){
$(this).closest('.parent').children().each(function(){
console.log('here');
$(this).children().toggle();
})
//$(this).closest('.parent').children().toggle(); //Doesn't work
});
Map<String,?> allPrefs = prefs.getAll();
for(Map.Entry<String,?> entry : allPrefs.entrySet()){
String key = entry.getKey();
String value = entry.getValue().toString();
}
但使用 public String[] fetchAllPreference(){
String[] values = new String();
int index = 0;
Map<String,?> allPrefs = prefs.getAll();
for(Map.Entry<String,?> entry : allPrefs.entrySet()){
value[index++] = entry.getValue().toString();
}
return values;
}
时效果很好但仅在与misfireThreshold
<add key="quartz.jobStore.misfireThreshold" value="60000" />
答案 0 :(得分:2)
由于事件传播,单击该按钮还会触发附加到parent
的点击事件,这将再次设置要显示的子项。
停止点击事件从button
传播将解决问题,如
$('.parent').on('click', function () {
$(this).children().show();
});
$('.hideParent').on('click', function (e) {
$(this).closest('.parent').children().hide();
e.stopPropagation();
});
演示:Fiddle
toggle
的最后一个解决方案并没有真正起作用,hideParent
点击处理程序没有做任何事情,因为parent
元素没有大孩子。实际的隐藏/显示是通过toggle
点击处理程序
parent
调用完成的