.removeAttr()
返回: jQuery
.removeAttr( attributeName )
描述:从匹配的元素集合中的每个元素上删除一个元素属性。
加入于: 1.0
.removeAttr( attributeName )- attributeName类型:String要删除的元素属性;自从jQuery 1.7,它可以是用空格分隔的元素属性的列表。
.removeAttr()方法使用了JavaScript函数removeAttribute(),但是它有一个好处是可以直接在一个jQuery对象上调用,而且它考虑跨浏览器不同的元素属性名。
注意:使用.removeAttr()方法来删除一个内联的onclick事件处理函数,在IE6、IE7、IE8中不会实现想要的效果。要想避免这种潜在的问题,请使用.prop()方法来代替它:
$element.prop( "onclick", null ); console.log( "onclick property: ", $element[ 0 ].onclick );
示例
点击按钮,来改变按钮后面的输入框的title。把鼠标指针移到文本输入框上面,以查看添加并删除元素属性title的效果。
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>removeAttr demo</title>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<button>Change title</button>
<input type="text" title="hello there">
<div id="log"></div>
<script>
(function() {
var inputTitle = $( "input" ).attr( "title" );
$( "button" ).click(function() {
var input = $( this ).next();
if ( input.attr( "title" ) === inputTitle ) {
input.removeAttr( "title" )
} else {
input.attr( "title", inputTitle );
}
$( "#log" ).html( "input title is now " + input.attr( "title" ) );
});
})();
</script>
</body>
</html>
演示结果