
使用此方法返回属性值时,将返回第一个匹配元素的值;使用此方法设置属性值时,将为选定元素设置一个或多个属性。
注意: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>效果图:

更多jQuery相关技术文章,请访问 jQuery答疑 栏目进行学习!!
以上就是jquery中prop()怎么用?的详细内容,更多请关注易知道|edz.cc其它相关文章!












