jQuery/HTML - 禁用 OnClick

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19320195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 23:41:08  来源:igfitidea点击:

jQuery/HTML - Disable OnClick

javascriptjqueryhtmlonclick

提问by user2534466

as the title says I'm trying to disable OnClick for specific

正如标题所说,我正在尝试禁用特定的 OnClick

<div id="test" style="display: block; opacity: 0.89;"></div>

this div is loaded from external script which is obfuscated so i cannot see the specific code.

这个div 是从外部脚本加载的,该脚本被混淆了,所以我看不到具体的代码。

What I have tried to remove this

我试图删除这个

$("#test").click(function(){ return false});
$("#test").unbind();
$("#test").removeAttr("onclick");
//Suggestions which do not work
$('#test').on('click', function(e) {e.preventDefault();return false;});
$("#test").click(function(event){
event.preventDefault();
});

None of the above work.

以上都不起作用。

EDIT: Solved my problem using .unbind("click"); after the div was created.

编辑:使用 .unbind("click"); 解决了我的问题;在创建 div 之后。

回答by Smeegs

You can add off("click");

你可以加 off("click");

Fiddle

小提琴

$(function () {
    $("#test").click(function(){alert('test');});
    $("#test").off('click');

});

this code will demonstrate removing a previously added click event.

此代码将演示删除先前添加的点击事件。

回答by Jose María Vallejo

Simple form, combination jquery with javascript.

简单的形式,结合 jquery 和 javascript。

// To disable:    

$('#test').each(function (){
    this.style.pointerEvents = 'none'; 
}); 

// To re-enable:
$('#test').each(function (){
    this.style.pointerEvents = 'auto'; 
}); 

So, you can change the selector for multiple tags

因此,您可以更改多个标签的选择器

回答by nnnnnn

None of the options you've tried would work if you use them before the div has been added to the page.So be sure to put your code somewhere after the other script that creates the div.

如果您在将 div 添加到页面之前使用它们,则您尝试过的所有选项都不起作用。因此,请务必将您的代码放在创建 div 的其他脚本之后的某个位置。

If more than one element had that same id that would also be a problem.

如果多个元素具有相同的 id,那也将是一个问题。

Regarding the individual methods you've tried:

关于您尝试过的各种方法:

$("#test").click(function(){ return false});
$("#test").unbind();
$("#test").removeAttr("onclick");

The first line will add a newclick handler to the element that prevents the default behaviour and stops the event propagating up,but it won't stop other event handlers bound to the element from running - especially if they run before your new handler, obviously.

第一行将向元素添加一个新的点击处理程序,以防止默认行为并停止事件传播但它不会阻止绑定到元素的其他事件处理程序运行 - 特别是如果它们在您的新处理程序之前运行,显然.

The second line will remove event handlers that were attached with jQuery, but not handlers attached by other means.

第二行将删除通过 jQuery 附加的事件处理程序,而不是通过其他方式附加的处理程序。

The third line should work to remove an inline onclickattribute if it exists, but not handlers added with jQuery.

第三行应该删除onclick存在的内联属性,而不是使用 jQuery 添加的处理程序。

Assuming you still can't stop the click behaviour even after ensuring your code runs after the div is added to the page, you could try something like the following:

假设即使在将 div 添加到页面后确保代码运行后仍然无法停止单击行为,您可以尝试以下操作:

var $test = $("#test").removeAttr("onclick"),
    $contents = $test.contents().detach();
$test.replaceWith($test.clone(false).append($contents));

Demo: http://jsfiddle.net/A9tmu/3/

演示:http: //jsfiddle.net/A9tmu/3/

The idea there is to replace the element with a clone of itself, because the cloned version (when created with .clone(false)) will not retain event handlers. I'm temporarily detaching the divs contents before cloning so that any child elements will get to keep their event handlers.

这里的想法是用自身的克隆替换元素,因为克隆版本(当使用 创build 时.clone(false))不会保留事件处理程序。我在克隆之前暂时分离了 div 内容,以便任何子元素都可以保留它们的事件处理程序。

回答by Jura Khrapunov

$('#test').on('click', function(e) {e.preventDefault();return false;});

回答by arvic.rivera

try this:

尝试这个:

$("#test").click(function(event) {
    event.preventDefault();
});

回答by Henk Jansen

Try this?

尝试这个?

$('#test').unbind('click')

OR

或者

   $( "#test" ).bind( "click", handler );
   $( "#test" ).unbind( "click", handler );

Try this

尝试这个

$( "#test" ).on("click", function () {
  });
$("#test").off("click");

回答by Ishan Jain

You can unbindclick event into clickevent:

您可以unbind单击事件进入click事件:

 $("#test").click(function(){
        $( "#test" ).unbind( "click");
        alert('Jquery/Javascript Event');
    });

Try JsFiddle

试试 JsFiddle