jQuery 禁用图像标签上的点击功能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8685127/
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-27 10:53:44  来源:igfitidea点击:

Disable onclick function on image tag

jquery

提问by user1096909

jQuery how can I disable a click on <img>tag

jQuery 如何禁用点击<img>标签

$("#ico_cal_id").attr('disabled', 'disabled') or
$("#img_id").attr('disabled', 'disabled') is not working

Below is my code:

下面是我的代码:

<span id="img_id"><src="ico_calendar.png" id="ico_cal_id" style="cursor: pointer"; onlick="showCalendar(this,'startDay','startMonth','startYear');" /></span>

whenever I click on the calendar_image the calendar is popping up

每当我点击 calendar_image 时,日历就会弹出

Any ideas?

有任何想法吗?

回答by Purag

Try this:

尝试这个:

$("#ico_cal_id").prop("onclick", false);

More about prop()here.

更多关于prop()这里

回答by alex

imgelements do not have a disabledattribute, so setting it won't affect its behaviour.

img元素没有disabled属性,因此设置它不会影响其行为。

You could prevent the event from bubbling and from executing other handlers with event.stopImmediatePropagation()...

您可以防止事件冒泡和执行其他处理程序event.stopImmediatePropagation()...

$("#ico_cal_id").click(function(event) {
    event.stopImmediatePropagation();
});

Alternatively, you could use $("#ico_cal_id").removeAttr("onclick")if the event is always being attached inline.

或者,$("#ico_cal_id").removeAttr("onclick")如果事件总是内联附加,您可以使用。

回答by karim79

Remove the onclick property?

删除 onclick 属性?

$("#ico_cal_id")[0].onclick = null;

or:

或者:

$("#ico_cal_id").removeAttr("onclick");

回答by Tom Stickel

These answers didn't work for me as I had unobtrusive jquery that bypassed that inline js function call.

这些答案对我不起作用,因为我有不显眼的 jquery 绕过了内联 js 函数调用。

I do not like "hack" code but for this image issue I did this

我不喜欢“hack”代码,但对于这个图像问题,我这样做了

HTML code

HTML代码

<input type="hidden" id="responseMode"/>

Code When to disable

代码何时禁用

$("#responseMode").val("response");

jquery img click

jquery img 点击

$("#myImageID").click(function() {
    if ($("#responseMode").val() === "response") {
        // ??
     } else {
        workedModalCall();
     }

});

Otherwise ALL of the other answered still allowed for my jquery click function to run and then the modal function call I didn't want to be called happened workedModalCall();

否则所有其他回答仍然允许我的 jquery 单击函数运行,然后我不想被调用的模态函数调用发生了 workModalCall();