attr()显示和隐藏元素的方法:1、使用“$("元素").attr("style","display:block");”语句显示元素;2、使用“$("元素").attr("style","display:none");”语句隐藏元素。

本教程操作环境:windows7系统、jquery1.10.2版本、Dell G3电脑。
在jquery中,可以利用attr()方法给元素的styles属性添加display样式来显示或隐藏元素。
display:block可显示元素-
display:none可隐藏元素
jQuery 1.12.4下载jQuery 1.12.4是jQuery 1.x系列的最后一个正式稳定版本,由jQuery团队于2016年发布。该版本主要面向需要兼容旧版浏览器环境的网站和Web应用,尤其适用于仍需支持Internet Explorer 6、Internet Explorer 7、Internet Explorer 8等老旧浏览器的项目。
实现代码:
html>
<meta><script></script><script>
$(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><div>hello</div>
<div>欢迎来到PHP中文网!</div>
<button>隐藏元素</button> <button>显示元素</button>

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










