javascript 使用 jquery 在 <div> 中禁用 <a href="">

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

Disabling <a href=""> inside a <div> using jquery

javascriptjqueryjquery-ui

提问by user2104391

Am trying to disable a hyperlink present inside a div tag using Jquery, i used the below codes, which are not helpful.

我正在尝试使用 Jquery 禁用 div 标签中存在的超链接,我使用了以下代码,这些代码没有帮助。

JQuery - 1.7v

jQuery - 1.7v

html Code -

html代码-

<div id="content">TESTING PURPOSE <(a)> href="/test/">Click Here <(a)> End </div>

jQuery - JS

jQuery - JS

$('#content').find("*").prop("disabled", true);

$('#content').prop("disabled", true);

回答by Denys Séguret

You can do this :

你可以这样做 :

$('#content a').click(function(){?return false });

Returning falsefrom the click event handler prevents the default behavior (following the link).

false从单击事件处理程序返回可防止默认行为(遵循链接)。

If you may have more than one link but you want to disable only this one, you may be more specific in your selector :

如果您可能有多个链接,但只想禁用这个链接,您可以在选择器中更具体:

$('#content a[href="/test/"]').click(function(){ return false });

回答by Michael J. Zoidl

Use preventDefaultto disable the default behavior of a link.

使用preventDefault禁用链接的默认行为。

Here is a little Code snippet:

这是一个小代码片段:

$('#content a').click(function(e) {
  // stop/prevent default behavior
  e.preventDefault();

  // do other stuff...
  // e.g. alert('Link is deactivated');
});

Here is a little jsFiddle example

这是一个小 jsFiddle 示例

Difference between e.preventDefaultand return false

e.preventDefaultreturn false 的区别

Source: Stackoverflow - jquery link tag enable disable

来源:Stackoverflow - jquery 链接标签启用禁用

回答by Ashish Kulshreshtha

You can also do this, just give div name of that anchor tag with remove function.

您也可以这样做,只需使用 remove 函数给出该锚标记的 div 名称。

$('.div_name a').remove();

回答by OmShankar

$('#content a').click(function(e) {

  e.preventDefault();

});

回答by OmShankar

$('.modal-body a').css({"pointer-events":"none"});