获取匹配的元素集中第一个元素的属性(property)值或设置每一个匹配元素的一个或多个属性(操作方式与attr相似):
随着一些内置属性的DOM元素或window对象,如果试图将删除该属性,浏览器可能会产生错误。jQuery第一次分配undefined值的属性,而忽略了浏览器生成的任何错误
如果第二个参数是callback,那么需要返回的是属性值,函数传参解释如下:
1:属性名称
2:返回属性值的函数,第一个参数为当前元素的索引值,第二个参数为原先的属性值
实例:
<!DOCTYPE html> <html lang=‘zh-cn‘> <head> <title>Insert you title</title> <meta http-equiv=‘description‘ content=‘this is my page‘> <meta http-equiv=‘keywords‘ content=‘keyword1,keyword2,keyword3‘> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <!-- <script type=‘text/javascript‘ src=‘./js/jquery-3.0.0.js‘></script> --> <script type=‘text/javascript‘ src=‘./js/jquery-1.12.1.min.js‘></script> <style type=‘text/css‘> #listDemo{list-style:none;} #listDemo li{width:350px;height:20px;margin:10px 0;background:red;text-align:center;font:400 13px/20px ‘Courier New‘;color:#FFF;} </style> <script type=‘text/javascript‘> $(function(){ $(‘#user‘).prop(‘autocomplete‘,function(index,oldAttr){ alert(oldAttr); }); }); </script> </head> <body> <input type=‘text‘ id=‘user‘ autocomplete="on" required="required"/> </body> </html>
时间: 2024-12-14 18:48:23