温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

jquery怎么实现不能点击元素

发布时间:2021-01-18 09:52:39 来源:亿速云 阅读:347 作者:小新 栏目:web开发

小编给大家分享一下jquery怎么实现不能点击元素,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

jquery实现不能点击元素的方法:首先添加一个按钮标签、一个div标签和一个锚标签;然后通过“$(this).attr("disabled", "disabled");”方法实现指定标签被禁用即可。

在许多情况下,基于某些条件并在执行某些操作后,我们需要将HTML按钮或输入标记设置为禁用或将其从网页中删除。 如果您正在寻找一种方法来使任何按钮不可点击,即使用jquery禁用按钮,那么您来对了地方。

例如,在按钮上单击调用jquery ajax函数直到其响应到来时,我们需要禁用该按钮(不可单击)。 将该按钮禁用以便用户不会一次又一次地按下该按钮是一种很好的做法。

在这篇文章中将解释如何禁用任何按钮,无论是Button标签,锚标签还是div,li元素。

HTML标记:添加按钮,div,一个标签

这里我们添加一个按钮标签,一个div标签和一个锚标签。 点击后,我们想让它不可点击(禁用)。

<div id="btnDiv">DIV CLICK</div>
<input id="btnButton" type="button" value="Button Click me" />
<a href="/" id="btnAnchr">Anchor Tag Click me</a>
Jquery代码:禁用HTML元素(div,button,anchor标签)
$("#btnButton").on('click', function () {
  // JQUERY
  $(this).attr("disabled", "disabled");
  // JavaScript
  document.getElementById("btnButton").setAttribute("disabled", "disabled");
  console.log("btn clicked");
});
$("#btnDiv").on('click', function () {
  $(this).off('click');
  document.getElementById("btnDiv").setAttribute("disabled", "disabled");
  console.log("Div clicked");
});
$("#btnAnchr").on('click', function (e) {
  $(this).attr("disabled", "disabled");
  e.preventDefault();
});

上述代码解释:当点击按钮或div后,该标签立即被禁用(不可点击)。

完整代码:

<html>
<head>
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
</head>
<body>
<br>
请用Chrome或Firefox浏览器访问该网页,按F12打开控制台(console),
然后点击下面的div或按钮,看日志(log)变化。
</br><br>
<div id="btnDiv" style="width:80px;background:#cccccc;padding:3;">
DIV CLICK
</div></br>
<input id="btnButton" type="button" value="Button Click me" /></br></br>
<a href="/" id="btnAnchr">Anchor Tag Click me</a></br></br>
<script type="text/javascript">
$("#btnButton").on('click', function () {
  // JQUERY
  $(this).attr("disabled", "disabled");
  // JavaScript
  document.getElementById("btnButton").setAttribute("disabled", "disabled");
  console.log("btn clicked ");
});
$("#btnDiv").on('click', function () {
  $(this).off('click');
  document.getElementById("btnDiv").setAttribute("disabled", "disabled");
  console.log("Div clicked ");
});
$("#btnAnchr").on('click', function (e) {
  $(this).attr("disabled", "disabled");
  e.preventDefault();
});
</script>
</body>
</html>

看完了这篇文章,相信你对“jquery怎么实现不能点击元素”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI