您的位置:首页 > 技术中心 > 前端框架 >

javascript怎么删除元素属性

时间:2021-04-27 20:02

javascript删除元素属性的方法:首先使用“document.getElementById("id值")”语句根据id值获取元素对象;然后使用“元素对象.removeAttribute("属性名称")”语句来删除元素的属性。

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

在javascript中,可以使用removeAttribute()方法来删除元素属性,语法:

removeAttribute("属性名")

示例:

<h1 style="color:red">Hello World</h1>

<p id="demo">点击按钮来删除标题中的 style 属性。</p>

<button onclick="myFunction()">试一下</button>

<script>
	function myFunction() {
		document.getElementsByTagName("H1")[0].removeAttribute("style");
	}
</script>

效果图:

1.gif

【推荐学习:javascript高级教程】

以上就是javascript怎么删除元素属性的详细内容,更多请关注www.gxlsystem.com其它相关文章!

本类排行

今日推荐

热门手游