Skip to content

.removeAttr()

.removeAttr( attributeName )

删除元素的属性

.remove( attributeName ) from @1.0

  • attributeName
    • 类型: String
    • 描述: 要删除的属性,支持空格分割。

.removeAttr()方法使用JavaScript的removeAttribute()函数,但它的优点是能够直接在 domtify 对象上调用,并且可以解决不同浏览器之间不同的属性命名问题。

例子:

有以下的html片段:

html
<input type="text" title="hello there" />

使用该方法删除元素的属性:

js
d("input").removeAttr("title")
// 支持空格分割删除多个
d("input").removeAttr("title type")

返回的结果:

html
<!-- 对应第一个只删除title -->
<input type="text" />

<!-- 同时删除了title和type -->
<input />

基于 MIT 许可发布