jQuery removeProp() 方法



jQuery 中的removeProp()方法用於從匹配元素集中刪除屬性。

如果要刪除 HTML 屬性(如 style、Id 或 checked),則需要使用 jQuery 的removeAttr()方法。

語法

以下是 jQuery 中 removeProp() 方法的語法:

$(selector).removeProp(property)

引數

此方法接受以下引數:

  • property: 要刪除的屬性的名稱。

示例

在以下示例中,我們使用 removeProp() 方法新增和刪除名為“code”的自定義屬性:

<html>
<head>
<style>
  img {
    padding: 10px;
  }
  div {
    color: red;
    font-size: 24px;
  }
</style>
<script src="https://code.jquery.com/jquery-3.7.0.js"></script>
<script>
  $(document).ready(function(){
      var para = $("p");
      
      // Setting a custom property
      para.prop("code", 1234);
      
      // Appending the property value to the paragraph
      para.append("The secret code is: ", String(para.prop("code")), ". ");
      
      // Removing the custom property
      para.removeProp("code");

      // Appending a message after removing the property
      para.append("Now the secret code is: ", String(para.prop("code")), ". ");
  });
  </script>
</head>

<body>
 <p></p>
</body>
</html>

執行 removeProp() 方法後,將刪除 code 屬性,並返回“undefined”。

jquery_ref_html.htm
廣告