jquery如何实现不能点击元素


本文摘自PHP中文网,作者藏色散人,侵删。

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

本教程操作环境:windows7系统、jquery1.10.0版本、Dell G3电脑。

推荐:jQuery视频教程

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

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

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

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

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

<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后,该标签立即被禁用(不可点击)。

完整代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

<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如何实现不能点击元素的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

jQuery如何按name属性选择元素

jQuery版本号如何隐藏

jQuery选择器是什么

jQuery中$是什么?

jQuery实现上传文件大小类型的验证例子

html中引用jQuery的两种方法

jQuery字母大小写转换函数有哪些

jQuery如何判断单选按钮radio是否选中

$.cookie is not a function

jQuery如何判断 input type="file"上传文件是否为空

更多相关阅读请进入《jQuery》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...