我的HTML代码是这样的:
<li data-id="<%=p.id %>" data-len="<%=products.length %>" data-count="<%=count %>">
<div class="pro_list_imgbox">
<img src="" />
</div>
</li>
我的js代码是这样的:
events: {
'click .pro_list_imgbox': 'loadPic',
'click li[data-id]': 'detailHandler'
},
loadPic: function (e) {
var target = $(e.target),
pic = target[0].nodeName === 'IMG' ? target : target.find('img');
if (!pic.data('loadState')) {
e.stopPropagation();
if (e.isPropagationStopped()) {
alert('stoped');
}
pic.attr('src', pic[0].src += '?t' + new Date().getTime());
}
},
detailHandler: function (e) {
alert('haha');
},
当我单击 img 元素时,我在函数 loadPic 中调用了 e.stopPropagation() ,并且调用成功。 但为什么detailHandler仍然执行并“哈哈”警报。 我该如何解决这个问题?
最佳答案
看来您的方向几乎是正确的。请检查以下是否可以解决您的问题:
events: {
'click .pro_list_imgbox': 'loadPic',
'click li[data-id]': 'detailHandler'
},
loadPic: function (e) {
var target = $(e.target),
pic = target[0].nodeName === 'IMG' ? target : target.find('img');
if (!pic.data('loadState')) {
pic.attr('src', pic[0].src += '?t' + new Date().getTime());
}
//this return statement will not allow the event to propagate to the parent element.
return false;
},
detailHandler: function (e) {
alert('haha');
},
关于javascript - 如何停止backbone.js中的传播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27688496/