Jquery 在共享公共空间的元素上避免双重事件触发器
我正在复制Google Reader中的一些功能,其中一个条目表由一个星号生成,允许用户突出显示某些行。我还希望用户能够单击该行以展开条目。要展开该行,我的jquery如下所示:Jquery 在共享公共空间的元素上避免双重事件触发器,jquery,Jquery,我正在复制Google Reader中的一些功能,其中一个条目表由一个星号生成,允许用户突出显示某些行。我还希望用户能够单击该行以展开条目。要展开该行,我的jquery如下所示: $(".action_row").click(function() { if( $(this).next().is(':hidden') ) { $(".detailed_row").hide(); $(".selected-action").removeClass("selec
$(".action_row").click(function() {
if( $(this).next().is(':hidden') ) {
$(".detailed_row").hide();
$(".selected-action").removeClass("selected-action");
$("#current-action").removeAttr("id");
$(this).next().toggle('fast').addClass("selected-action");
$(this).addClass("selected-action").attr("id", "current-action");
} else {
$(".selected-action").removeClass("selected-action");
$("#current-action").removeAttr("id");
$(".detailed_row").hide();
}
});
单击过程中发生什么并不重要,只是单击.action行触发事件
但在每个动作行中都有一颗星星:
$(".action-star").click(function() {
//Toggle the star through CSS
$(this).toggleClass("star-checked");
if ($(this).hasClass("star-checked")) {
$(this).html("<i class='icon-star icon-large'></i>");
} else {
$(this).html("<i class='icon-star-empty icon-large'></i>");
}
});
$(“.action star”)。单击(函数(){
//通过CSS切换星号
$(此).toggleClass(“星号选中”);
if($(this).hasClass(“星号选中”)){
$(this.html(“”);
}否则{
$(this.html(“”);
}
});
如果用户单击星号,我只希望切换星号。但是,现在,当单击星号时,它会触发星号切换和action_row事件
我应该如何更改代码,以便在单击星号时不使用操作行代码?阅读和
这件事似乎是从明星涌向了动作排,这会给你带来不想要的影响
$(“.action star”)。单击(函数(e){
e、 stopPropagation();//太好了。但是动作明星先开火的保证是什么呢?因为它“在顶部”,所以这是确定的吗?您说过动作星
在动作行
内。因此事件会冒泡-通过使用停止冒泡
,它会阻止事件冒泡到动作行
,从而防止在您单击动作星
时发生在动作行
上的单击事件
$(".action-star").click(function(e) {
e.stopPropagation(); // <-- top stop events from bubbling up
//Toggle the star through CSS
$(this).toggleClass("star-checked");
if ($(this).hasClass("star-checked")) {
$(this).html("<i class='icon-star icon-large'></i>");
} else {
$(this).html("<i class='icon-star-empty icon-large'></i>");
}
});