我是靠谱客的博主 酷酷飞鸟,这篇文章主要介绍jquery attr()怎么显示隐藏元素,现在分享给大家,希望可以做个参考。

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

在jquery中,可以利用attr()方法给元素的styles属性添加display样式来显示或隐藏元素。

  • display:block可显示元素

  • display:none可隐藏元素

实现代码:

复制代码
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
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="js/jquery-1.10.2.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $("#but1").click(function() { $("div").attr("style","display: none;"); }); $("#but2").click(function() { $("div").attr("style","display: block;"); }); }); </script> <style> div{ margin: 10px 0; } </style> </head> <body> <div>hello</div> <div>欢迎来到靠谱客!</div> <button id="but1">隐藏元素</button> <button id="but2">显示元素</button> </body> </html>
登录后复制

1.gif

相关教程推荐:jQuery视频教程

以上就是jquery attr()怎么显示隐藏元素的详细内容,更多请关注靠谱客其它相关文章!

最后

以上就是酷酷飞鸟最近收集整理的关于jquery attr()怎么显示隐藏元素的全部内容,更多相关jquery内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(106)

评论列表共有 0 条评论

立即
投稿
返回
顶部