jQuery :button 選擇器



jQuery 中的:button 選擇器用於選擇所有<button> 元素和型別為“button”的 <input> 元素。

語法

以下是 jQuery :button 選擇器的語法:

$(":button")

引數

此選擇器將選擇所有按鈕元素。

示例 1

在以下示例中,我們使用“jQuery :button”選擇器來選擇所有按鈕元素:

<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            $(":button").css("color", "blue");
        });
    </script>
</head>
<body>
    <button>Button Element</button>
    <input type="button" value="Input Button">
</body>
</html>

執行上述程式後,所有按鈕元素的文字顏色將更改為藍色。

示例 2

在這裡,我們選擇所有按鈕元素並在聚焦時新增一個點選事件:

<html lang="en">
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            // Select all button elements and add a click event
            $(":button").click(function(){
                alert("Button clicked!");
            });
        });
    </script>
</head>
<body>
    <button>Click Me</button>
    <input type="button" value="Click Me Too">
</body>
</html>

當我們點選按鈕元素時,螢幕上將顯示一個警報。

jquery_ref_selectors.htm
廣告

© . All rights reserved.