jquery怎么移除style属性

青灯夜游
青灯夜游原创
2022-05-16 15:30:145581浏览

在jquery中,可以利用removeAttr()方法来移除style属性,该方法的作用就是从被选元素中移除指定属性,只需要将该方法的的参数设置为“style”即可;语法为“$(selector).removeAttr("style")”。

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

style属性是HTML核心属性,用于为元素指定内联样式(inline style)。

style属性将覆盖其他全局样式,比如在<style>元素或外部样式表中定义的样式。

那么利用jquery怎么移除style属性

在jquery中,可以利用removeAttr()方法来移除style属性。

removeAttr() 方法用于从被选元素中移除属性。

语法:

$(selector).removeAttr(attribute)

attribute:必需。规定从指定元素中移除的属性。

示例:移除第一个 p 元素的style属性

<!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() {
				$("button").click(function() {
					$("p:first").removeAttr("style");
				});
			});
		</script>

	</head>

	<body>
		<h1>这是一个大标题</h1>
		<p style="font-size: 120%;color: blue;background-color: yellow;">这是一个段落。</p>
		<p style="font-size: 120%;color: blue;background-color: yellow;">这是另一个段落。</p>
		<button>移除第一个 p 元素的style属性</button>
	</body>

</html>

1.gif

【推荐学习:jQuery视频教程web前端开发视频

以上就是jquery怎么移除style属性的详细内容,更多请关注php中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。