简体   繁体   English

jQuery防止单击时传播行的子链接

[英]jQuery prevent children links of row from being propagated when clicked

I have a table with classes on some of the rows. 我在某些行上有一个带有类的表。 All the rows have links and when I click on the actual row I want to execute a function but prevent all of the links from being propagated. 所有行都有链接,当我单击实际行时,我想执行一个函数,但阻止所有链接传播。

Using e.stopPropagation() on my functions is working however one of the links has an onclick function and href=javascript:void(0); 在我的函数上使用e.stopPropagation()可以正常工作,但是其中一个链接具有onclick函数,并且href=javascript:void(0); in it so it's just bypassing my e.stopPropagation statement and continues to run the procedures of the row-clicking function. 在其中,它只是绕过我的e.stopPropagation语句,并继续运行行单击功能的过程。

Here's my code: 这是我的代码:

$('[class*=tooltip-properties]').click(function() { // this is the table row
    // do stuff
});
$('[class*=tooltip-properties] a').click(function(e) {
    e.stopPropagation();
});

And the link causing trouble: 和造成麻烦的链接:

<a id="tageditlink25730" title="Edit" onclick="ame_ajax_form_tags(25730, 'LM');return false;" href="javascript:void(0);">

The piece of code bypassing your jQuery handlers is the onclick="... return false;" 绕过您的jQuery处理程序的代码段是onclick="... return false;" attribute. 属性。

jQuery will not override inline event handlers, and inline event handlers will be executed before any jQuery event handler (see Will JQuery override existing inline event handlers? ). jQuery不会覆盖内联事件处理程序,并且内联事件处理程序将任何jQuery事件处理程序之前执行(请参阅JQuery会覆盖现有的内联事件处理程序吗? )。 This particular onclick handler will prevent the execution of any subsequent handlers, because it returns false . 这个特定的onclick处理程序将阻止执行任何后续处理程序,因为它返回false

If you have control over the server side code, you can remove the return false part in the generated page, or (even better) remove the onclick attribute and bind all your handlers using jQuery. 如果您可以控制服务器端代码,则可以在生成的页面中删除return false部分,或者(甚至更好)删除onclick属性并使用jQuery绑定所有处理程序。

[Edit] [编辑]

If you can't, one way is to add a child node inside the a tag, and bind a click handler on that new node : 如果你不能,一个办法是增加一个子节点内的a标签,并结合新的节点上单击处理程序:

$('[class*=tooltip-properties] a').each(function(){
    var html = $(this).html();
    $(this).html( '<span class="wrapper">'+html+'</span>' );
});

$('[class*=tooltip-properties] span.wrapper').click(function(){
    // do stuff
});

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

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