简体   繁体   English

单击jQuery按钮内的span

[英]Click span inside jQuery button

Part of a bigger solution, I have accordion-type jQuery control that implements headers as jQuery toggle buttons. 作为更大解决方案的一部分,我拥有可折叠类型的jQuery控件,该控件将标头实现为jQuery切换按钮。 Each header also has to have a help balloon. 每个标题也必须有一个帮助提示框。 In nutshell, the setup is similar to that posted at this jsFiddle . 简而言之,该设置类似于此jsFiddle上发布的设置。

<div id="button1">Go to main action <span id="span1" style="color:blue">Help</span>     </div>
<div id="out"/>
$('#button1').button().click(function(){
    $('#out').text('Button clicked');
});

$('#span1').click(function(){
    $('#out').text('Span clicked');
});

Is that possible to make it so I can click on the Help span, which is located inside the button div? 是否可以做到这一点,所以我可以单击按钮div内的“帮助”范围? Or, will button always get the events for all of its content preventing inside elements from getting click events? 或者,按钮将始终获取其所有内容的事件,从而阻止内部元素获取点击事件吗?

Thank you. 谢谢。

Your event first fires on span and then also on button . 您的事件首先在span上触发,然后在button上触发。 You can prevent the event to be fired on parent by stopPropagation() method. 您可以通过stopPropagation()方法stopPropagation()在父对象上触发stopPropagation() Also, return false do the same. 另外, return false也是一样。

Fiddle for demonstration 小提琴示范

$('#span1').click(function(e){
    $('#out').text('Span clicked');
    e.stopPropagation();
});

or: 要么:

$('#span1').click(function(){
    $('#out').text('Span clicked');
    return false;
});

jSfiddle http://jsfiddle.net/kxntf/5/ jSfiddle http://jsfiddle.net/kxntf/5/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM