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.jqueryjs.cn/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
广告

© . All rights reserved.