【问题标题】:Disabled attribute for anchor tag not working [duplicate]锚标记的禁用属性不起作用[重复]
【发布时间】:2018-07-17 22:08:22
【问题描述】:

我有一个要求,我需要在 href 标记上禁用 onclick 事件。我可以禁用“a href”标签,但“a href”标签中的 onclick 事件仍然没有被禁用。

下面的 sn-p 更清楚地解释了这个问题!

//click works on button even though it is disabled 
function check(){
alert("i am able to click on button though it is disabled")
}
<meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>


<a href="#" id="tr" onclick="check()" class="btn btn-warning" disabled>Click</a>

在上面的 sn-p 中,即使“a href”似乎被禁用,但是当你点击 a-href 时,点击仍然有效!

请帮帮我!

提前致谢!

【问题讨论】:

  • 您可以尝试 unbind() 从链接中删除点击事件,或者您可以从 check() 函数中返回 false。
  • 您应该通过调用 event.preventDefault(); 来防止链接的默认行为;事件被传递到事件处理程序中。

标签: javascript html


【解决方案1】:

这是因为 disabled 属性只对按钮元素起作用,所以将你的锚点修改为按钮:

<button type="button" id="tr" onclick="check()" class="btn btn-warning" disabled>Click</button>

【讨论】:

    猜你喜欢
    • 2023-03-31
    • 1970-01-01
    • 2011-09-12
    • 2016-06-04
    • 1970-01-01
    • 1970-01-01
    • 2013-09-13
    • 2014-07-12
    相关资源
    最近更新 更多