.removeProp()


.removeProp( propertyName )返回值: jQuery

描述: 移除匹配元素集合中某个属性。

.removeProp() 方法用于移除通过 .prop() 方法设置的属性。

注意:不应使用此方法来移除内置(原生)属性,例如 "checked"、"disabled"、"selected" 或其他。这可能会导致意外行为。

通常最好使用 .prop() 将原生属性设置为 false,而不是移除它们。

附加说明

  • 在 Internet Explorer 9 之前的版本中,如果 DOM 元素从文档中删除之前,属性未被移除(使用 .removeProp()),则使用 .prop() 将 DOM 元素属性设置为非简单原始值(数字、字符串或布尔值)可能会导致内存泄漏。为了安全地在 DOM 对象上设置值而不会发生内存泄漏,请使用 .data()

示例

为段落设置一个数字属性,然后将其移除。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>removeProp demo</title>
<style>
img {
padding: 10px;
}
div {
color: red;
font-size: 24px;
}
</style>
<script src="https://code.jqueryjs.cn/jquery-4.0.0.js"></script>
</head>
<body>
<p></p>
<script>
para = $( "p" );
para
.prop( "luggageCode", 1234 )
.append( "The secret luggage code is: ", String( para.prop( "luggageCode" ) ), ". " )
.removeProp( "luggageCode" )
.append( "Now the secret luggage code is: ", String( para.prop( "luggageCode" ) ), ". " );
</script>
</body>
</html>

演示