當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


JQuery prop()用法及代碼示例

prop()是 jQuery 中的內置方法,用於設置或返回所選元素的屬性和值。當此方法用於返回屬性值時,它返回第一個匹配元素的值;當此方法用於設置屬性值時,它為所選元素設置一個或多個屬性。

用法:

$(selector).prop(para1, para2)

參數:它接受下麵指定的兩個參數 -

  • para1:指定屬性。
  • para2:指定設置時屬性的值。
  • 返回值:它返回所選元素的屬性和屬性值集。
    顯示 prop() 方法工作原理的 jQuery 代碼:

html


<!DOCTYPE html>
<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>

輸出:

jQuery-99



相關用法


注:本文由純淨天空篩選整理自kundankumarjha大神的英文原創作品 jQuery prop() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。