菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
47
0

jquery中prop()怎么用?

原创
05/13 14:22
阅读数 1185

使用此方法返回属性值时,将返回第一个匹配元素的值;使用此方法设置属性值时,将为选定元素设置一个或多个属性。

注意:prop() 方法应该用于检索属性值,例如 DOM 属性(如 selectedIndex, tagName, nodeName, nodeType, ownerDocument, defaultChecked, 和 defaultSelected)。

提示:如需检索 HTML 属性,请使用 attr() 方法代替。

提示:如需移除属性,请使用 removeProp() 方法。

语法:

$(selector).prop(para1, para2)

参数:它接受下面指定的两个参数-

  • para1:指定属性。

  • para2:如果设置,则指定属性的值。

返回值:返回所选元素的属性和为属性设置的值。

示例:

<!DOCTYPE html>
<html> 
   <head> 
   	<meta charset="UTF-8">
      <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"> </script> 
      <!-- jQuery code to show thre working of this method -->
      <script> 
         $(document).ready(function(){ 
             $("button").click(function(){ 
             var $x = $("p"); 
             $x.prop("color", "green"); 
             $x.append("属性为color,其值为:" + $x.prop("color")); 
                   
             }); 
         }); 
      </script> 
      <style> 
         div{ 
         width: 250px; 
         padding: 10px; 
         height: 120px; 
         border: 2px solid green; 
         } 
      </style> 
   </head> 
   <body> 
      <div> 
         <p></p> 
         <br><br> 
         <!-- click on button this method -->
         <button>Click Here!</button> 
      </div> 
   </body> 
</html>

效果图:

1.gif

发表评论

0/200
47 点赞
0 评论
收藏
为你推荐 换一批