.removeProp()


.removeProp( propertyName )傳回:jQuery

說明:移除一組已配對元素的屬性。

.removeProp() 方法會移除由 .prop() 方法設定的屬性。

注意:此方法不應使用於移除內建(原生)屬性,例如「checked」、「disabled」、「selected」或其他屬性。這可能會導致意外行為。

幾乎總是建議使用 .prop() 將原生屬性設定為 false,而不是移除它們。

其他說明

  • 在版本 9 之前的 Internet Explorer 中,使用 .prop() 將 DOM 元素屬性設定為非單純基本值(數字、字串或布林值)時,如果在從文件移除 DOM 元素之前未移除屬性(使用 .removeProp()),可能會造成記憶體外洩。若要在 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.jquery.com/jquery-3.7.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>

示範