Javascript 如何防止父级上的嵌套引导折叠元素激发.show()?
这是我的设置的一部分 您会注意到,我在父手风琴上有指示箭头,并且有一个函数,在手风琴显示和隐藏时切换它们。问题在于内部折叠区域(用于隐藏产品详细信息)触发切换父级上箭头的功能。似乎当子元素实际上是正在折叠的元素时,必须在父元素collapse上触发.show()事件 如何修复此问题,使子折叠不会切换父级上的箭头 这是我函数的代码Javascript 如何防止父级上的嵌套引导折叠元素激发.show()?,javascript,jquery,twitter-bootstrap,jsfiddle,Javascript,Jquery,Twitter Bootstrap,Jsfiddle,这是我的设置的一部分 您会注意到,我在父手风琴上有指示箭头,并且有一个函数,在手风琴显示和隐藏时切换它们。问题在于内部折叠区域(用于隐藏产品详细信息)触发切换父级上箭头的功能。似乎当子元素实际上是正在折叠的元素时,必须在父元素collapse上触发.show()事件 如何修复此问题,使子折叠不会切换父级上的箭头 这是我函数的代码 $('.collapse').on('show', function(){ $(this).parent().find(".icon-chevron-right").
$('.collapse').on('show', function(){
$(this).parent().find(".icon-chevron-right").removeClass("icon-chevron-right").addClass("icon-chevron-down");}).on('hide', function(){
$(this).parent().find(".icon-chevron-down").removeClass("icon-chevron-down").addClass("icon-chevron-right");});
只需使用
event.stopPropagation
停止事件传播到按钮单击上的父级,这样它就不会在按钮上执行单击事件后触发手风琴上的单击事件
$thisButton.click(function(e) {
e.stopPropagation();
//.. Code follows
另外,您不需要在选择器上执行each来绑定click事件,只需为click事件本身指定选择器即可
$(".btn-switch").click(function (e) {
e.stopPropagation();
var $thisButton = $(this);
$thisButton.toggleClass("btn-primary btn-danger");
if ($(this).hasClass('btn-danger')) {
$thisButton.html('<i class="icon-remove-circle icon-white"></i> Remove');
$(this).parents('.thumbnail').find('.rental-count').val('1');
$(this).parents('.thumbnail').find('input').change();
} else {
$thisButton.html('<i class="icon-ok-circle icon-white"></i> Add to Quote');
$(this).parents('.thumbnail').find('input').val('');
$(this).parents('.thumbnail').find('input').prop('checked', false);
$(this).parents('.thumbnail').find('input').change();
}
});
$(“.btn开关”)。单击(功能(e){
e、 停止传播();
变量$thisButton=$(此);
$thisButton.toggleClass(“btn主btn危险”);
if($(this).hasClass('btn-danger')){
$thisButton.html('Remove');
$(this).parents('.thumbnail').find('.rental count').val('1');
$(this).parents('.thumbnail').find('input').change();
}否则{
$thisButton.html('addtoquote');
$(this).parents('.thumbnail').find('input').val('');
$(this).parents('.thumbnail').find('input').prop('checked',false);
$(this).parents('.thumbnail').find('input').change();
}
});
最好的方法是使用
$(".btn-switch").click(function (e) {
//you code
return false;
});
谢谢这是一个显而易见的解决办法。我以前试过的时候一定是语法错误-/@JosiahSprague你以前可能试过每次回叫吗?这有效吗?我以前尝试过函数(){event.stopPropagation()}。你可以看出我没有花太多时间在JavaScript上。它现在正在使用您的解决方案。