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


JQuery blur()用法及代碼示例

jQuery blur()是一種內置方法,用於從所選元素中刪除焦點。此方法啟動模糊事件,或者可以附加一個函數以在模糊事件發生時運行。

用法:

$(selector).blur(function)

參數:

它接受可選參數“function”。

jQuery 示例展示 blur() 函數的工作原理:

示例 1:在下麵的代碼中,沒有函數傳遞給模糊方法。

HTML


<!DOCTYPE html>
<html>
<head>
    <script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
    </script>
    <script>
        $(document).ready(function () {
            $("#btn").click(function () {
                $("input").blur();
                $("p").html(
                    "This is blur method that is used!!!");
            });
        });
    </script>
</head>
<body>
    <!--enter value and click on the button -->
    Enter Value:
    <input type="text">
    <br><br>
    <button id="btn">
        start blur event!!!
    </button>
    <p style="color:green"></p>
</body>
</html>

輸出:

示例 2:在下麵的代碼中,函數被傳遞給模糊方法。

HTML


<!DOCTYPE html>
<html>
   
<head>
    <script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
      </script>
   
    <script>
        <!--jQuery code to show blur method-->
            $(document).ready(function () {
                $("input").blur(function () {
                    $(this).css("background-color", "#ADFF2F");
                });
            });
    </script>
</head>
   
<body>
    <!-- Enter a value to the field and click outside
        to see the change -->
    Enter Value:
    <input type="text" name="fullname">
</body>
   
</html>

輸出:



相關用法


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