JQuery的removeProp()与removeAttr()移除属性的区别

发布时间:2018-12-12编辑:佚名阅读(1693)

※ removeProp()【少用删除,将属性值设置为false即可】

removeProp()方法移除由prop()方法设置的属性。

注意:removeProp()会移除当前jQuery对象所匹配的每一个元素上指定名称的属性。

语法

jQuery 1.6 新增该函数。removeProp()函数的语法如下:

jQueryObject.removeProp( propertyName )

注意:removeProp()会移除当前jQuery对象所匹配的每一个元素上指定名称的属性。

返回值

removeProp()函数的返回值是jQuery类型,返回当前jQuery对象本身。

window对象或DOM元素的一些内置属性是不允许删除的,如果试图删除这些属性,将会导致浏览器产生一个错误。jQuery首先会将该属性的值赋为undefined,并忽略掉浏览器生成的任何错误信息。

一般情况下,你最好使用该函数(即removeProp())来删除一些自定义的属性,而不是内置属性。

请不要使用本函数(即removeProp())来删除DOM元素的本地属性checked、selected和disabled。这将彻底删除对应的属性,并且,一旦删除之后,你无法再向该DOM元素重新添加对应的属性。请使用prop()函数将其设为false即可,例如:jQueryObject.prop("checked",false)。

移除onclick事件:

在IE6 ~ IE8中,removeAttr()函数无法移除行内的onclick事件属性,为了避免潜在的问题,请使用prop()函数,相关代码如下:jQueryObject.prop("onclick",null);

示例与说明:

removeProp()使用例子:

<div id="n1">
      <p id="n2" class="demo test" data-key="UUID" data_value="1235456465">CodePlayer</p>
</div>

JQuery代码如下:

//在当前页面内追加换行标签和指定的HTML内容
function w( html ){
    $(document.body).append("<br/>" + html);
}
var $n2 = $("#n2");
$n2.prop("prop_a", "CodePlayer");
$n2.prop( "prop_b", { name: "CodePlayer", age: 20 } );
w( $n2.prop("prop_a") ); // CodePlayer
w( $n2.prop("prop_b") ); // [object Object]
// 删除n2 Element对象上的属性prop_a和prop_b
$n2.removeProp("prop_a");
$n2.removeProp("prop_b");
w( $n2.prop("prop_a") ); // undefined
w( $n2.prop("prop_b") ); // undefined
// removeProp()只是删除元素(Element对象)自身的data-key属性
// 由于其自身并没有data-key属性,因此不会删除任何属性
// 也不会对上面id为n2的元素节点(HTML文档)的data-key属性造成影响
$n2.removeProp("data-key");
w( $n2.prop("data-key") ); // undefined
// 通过attr()访问元素文档节点中的属性
w( $n2.attr("data-key") ); // UUID

※ removeAttr() 定义和用法

removeAttr()函数用于移除在当前jQuery对象所匹配的每一个元素节点上指定的属性。

该函数属于jQuery对象(实例)。如果你需要设置或获取元素节点上的某些属性值,你可以使用attr()函数。

语法

removeAttr()函数的语法如下:

jQueryObject.removeAttr( attributeNames );

注意:removeAttr()会移除当前jQuery对象所匹配的每一个元素上指定名称的属性。

jQuery 1.7 新增支持:你可以传入以空格分隔的字符串,空格隔开的每个子字符串即是需要移除的属性名称。

<div id="n1">
    <img id="n2" data-id="12" alt="站点名称" title="CodePlayer" src="/image/blank.gif" >
    <img id="n3" data-id="15" alt="站点logo" title="专注于编程开发技术分享" src="http://localhost/static/image/site-url.png" >
</div>

JQuery代码如下:

var $imgs = $("img");
// 移除所有img元素的data-id属性
$imgs.removeAttr("data-id");
var $n2 = $("#n2");
var $n3 = $("#n3");
document.writeln( $n2.attr("data-id") ); // undefined
document.writeln( $n3.attr("data-id") ); // undefined
// 从jQuery 1.7开始,可以同时移除alt和title属性
// 在jQuery 1.6之前的某些版本中,属性名称包含空格会抛出错误"Uncaught InvalidCharacterError:
// The string contains invalid characters. "
// 在jQuery 1.6.x中,属性名称包含空格,不会抛出错误,而是忽略掉本次移除操作
$imgs.removeAttr("alt title");
document.writeln( $n2.attr("alt") ); // undefined (站点名称){小括号内表示在jQuery 1.6.x中的
                                      //输出内容,下同}
document.writeln( $n2.attr("title") ); // undefined (CodePlayer)
document.writeln( $n3.attr("alt") ); // undefined (站点logo)
document.writeln( $n3.attr("title") ); // undefined (专注于编程开发技术分享)


  关键字:JQueryremoveProp()removeAttr()移除属性区别


鼓掌

0

正能量

0

0

呵呵

0


评论区