javascript怎么修改元素的style属性-LMLPHP

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

在javascript中,可以利用setAttribute()方法来修改style属性。

setAttribute() 方法添加指定的属性,并为其赋指定的值。如果这个指定的属性已存在,则仅设置/更改值。

语法:

element.setAttribute(attributename,attributevalue)
登录后复制
参数类型描述
attributenameString必需。您希望添加的属性的名称。
attributevalueString必需。您希望添加的属性值。

示例:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
	</head>
	<body>
		<div id="box" style="background-color: black;color: white;border: 2px solid red;">一个div元素</div><br />
		<button onclick="replaceMessage()"> 修改style属性的值</a>

			<script type="text/javascript">
				function replaceMessage() {
					var div = document.getElementById("box"); 
					div.setAttribute("style","background-color: #FFC0CB;color: black;border: 3px dashed peru;");
				}
			</script>

	</body>
</html>
登录后复制

javascript怎么修改元素的style属性-LMLPHP

【相关推荐:javascript学习教程

以上就是javascript怎么修改元素的style属性的详细内容,更多请关注Work网其它相关文章!

09-18 20:56