jQuery prop()的例子
prop()是jQuery内置的一个方法,用于设置或返回所选元素的属性和值。当这个方法被用来返回属性值时,它返回第一个匹配的元素的值,当这个方法被用来设置属性值时,它为选定的元素设置一个或多个属性。
语法 :
$(selector).prop(para1, para2)
参数:它接受两个参数,具体如下–**
- para1:它指定了属性。
- para2:在设置的情况下,它指定了属性的值。
返回值:它返回所选元素的属性和为该属性设置的值。
jQuery代码显示prop()方法的工作:
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<!-- jQuery code to show the working of this method -->
<script>
(document).ready(function(){
("button").click(function(){
var x =("p");
x.prop("color", "green");
x.append(" Property is color and its value: " + $x.prop("color"));
});
});
</script>
<style>
div{
width: 250px;
padding: 10px;
height: 100px;
border: 2px solid green;
}
</style>
</head>
<body>
<div>
<p></p>
<br><br>
<!-- click on button this method -->
<button>Click Here!</button>
</div>
</body>
</html>
输出:
在点击 “点击这里 “按钮之前-
在点击 “点击这里 “按钮后-