我正在处理一个嵌套的单击事件(有大量的图像,一些有子图像,一些没有)。
对于所有的图像(子项、父项和非父项),它们都应该显示在一个不同的css div中,然后如果再次单击,就会无限期地删除它们自己。
到目前为止,这段代码适用于父图像和非父图像,但是冒泡是使子图像显示父图像。
如果我在子函数之前返回false来停止冒泡,它将破坏数组中更低的父图像和非父图像的功能(子图像在新的弹出css中,而不是列表中),但是如果我不停止冒泡,嵌套的click事件将不会运行。
有没有人看到解决这个烂摊子的好办法?
// Click an image, and it will change it in the demo css class panel
$('.item-button').data('status','not_clicked');
$('.item-button').click(function(event) {
var layerID = $(this).attr('data-layer'); //these do not correspond to actual layers of nesting.
var itemID = $(this).attr('data-item');
var itemmulti = $(this).attr('data-multi'); //this value def works to recognize parent from other non-parent items (not children).
var clayerID = $(this).attr('data-clayer');
var citemID = $(this).attr('data-citem'); //child item 1.
if(( $(this).data('status') == 'clicked') || itemID == 0) {
var imageSrc = $(this).parent().find('img').attr('id');
$(layerID).attr('src', imageSrc);
} else {
$(this).data('status','clicked');
var imageSrc = $(this).parent().find('img').attr('id');
$(layerID).attr('src', imageSrc);
if (itemmulti != 0) {
//begin headache.
document.getElementById('child-' + itemID).style.display = "inline-block";
//this is where a separate click function for children items begins.
$('.item-sub1').data('status','unclicked');
$('.item-sub1').click(function(event) {
if(( $(this).data('status') == 'click') || citemID == 0) {
var imageSrc = $(this).parent().find('img').attr('id');
$(selector).attr('src', imageSrc);
} else {
$(this).data('status','click');
var imageSrc = $(this).parent().find('img').attr('id');
$(clayerID).attr('src', imageSrc);
}
return false;
});
}
}
});<img button title id alt />
<input radio id="layer-{ID}-{item.ID}-radio" name="layer-{ID}" value="{item.ID}" class="item-button" data-multi="{item.PARENT}" data-layer="{ID}" data-item="{item.ID}" />
<!-- IF item.CHILD1 != 0 -->
<div id="child-{item.ID}" style="width: 300px; position: absolute; display: none;">
<img button here title data-layer="{item.CLAYER1}" data-item="{item.CHILD1}">
<input radio id="layer-{item.CLAYER1}-{item.CHILD1}-radio" name="layer-{item.CLAYER1}" value="{item.CHILD1}" style="display: none;" class="item-sub1" data-clayer="{item.CLAYER1}" data-citem="{item.CHILD1}" />发布于 2013-01-04 19:56:16
我认为您需要做的是使用stopPropagation()方法来防止事件在DOM树上冒泡,并防止任何父处理程序收到事件通知。
$('.item-button').click(function(event) {
event.stopPropagation();
// your code...更多信息: 1. jquery 2. javascript
https://stackoverflow.com/questions/14156287
复制相似问题