JQuery:禁用click事件 [英] JQuery: Disable click event

查看:90
本文介绍了JQuery:禁用click事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想禁用我页面上的所有点击事件,但有些事件仍然被调用,假设我有html如下

I want to disable all click events on my page but some of events are still getting called, suppose I have html as below

<div id='parent'>
    <table>
        <tr>
            <td><input type = 'text'/></td>
            <td><select><option>A</option><option>B</option></select></td>
            <td><a href='#' onclick="alert('Called')">Click </a></td>
        </tr>
        <tr>
            <td>dscsdcd</td>
            <td>dscsdcd</td>
            <td>dscdscdsc</td>
        </tr>
        <tr>
            <td>sdcdsc</td>
            <td>dssdcdsc</td>
            <td><input type='submit' value='Button'/></td>
        </tr>
    </table>
</div>

脚本如下

$('#parent').click( function(e)
{
     e.stopPropagation();
     e.preventDefault();
     e.stopImmediatePropagation();
     return false;
});

当我点击锚标记时,它会提醒被叫它不应该因为我为它的父母禁用事件。看起来锚标签重写 onclick ,如果是,那么如何防止它?如果没有,那么可能是什么解决方案?

when I click anchor tag, it alerts Called which it shouldn't because I am disabling event for it's parent. Looks like anchor tag is overriding onclick, if it is then how to prevent it? if not, then what could be the solution?

JS Fiddle

推荐答案

onclick 属性在这里具有更高的优先级。您可以循环< a> 元素并取消设置此属性:

onclick attribute has a higher priority here. You can loop your <a> elements and unset this attribute:

$('#parent a').each(function () {
  $(this).attr('onclick', '');
}).click(function () {
  // ...
});

这篇关于JQuery:禁用click事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆