jQuery event.result 屬性



jQuery 的event.result 屬性用於檢索由指定事件觸發的事件處理程式返回的最後一個或上一個值。

此屬性包含同一事件的上一個事件處理程式返回的值。如果上一個處理程式沒有返回值,則event.result 將為undefined

語法

以下是 jQuery event.result 屬性的語法:

event.result

引數

此屬性不接受任何引數。

返回值

此屬性返回事件處理程式返回的最後一個或上一個值。

示例 1

以下是 jQuery event.result 屬性的基本示例:

<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
</head>
<body>
    <p>Click on the below button to see the event result</p>
    <button>Click me!</button>
    <script>
        $('button').click(function(){
            return "TutorialsPoint";
        });
        $('button').click(function(event){
            alert("Eevnt result: " + event.result);
        })
    </script>
</body>
</html>

輸出

以上程式顯示一個按鈕,當單擊該按鈕時,會在瀏覽器螢幕上彈出一個警報,顯示事件結果:


單擊按鈕時:


示例 2

以下是 jQuery event.result 屬性的另一個示例。我們使用此屬性來檢索由指定元素觸發的事件處理程式的返回值:

<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
    <style>
        button{
            background-color: green;
            color: white;
            padding: 10px
        }
        span{
            color: green;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <p>Click on the below button to see the event result</p>
    <button>Click to see event result</button>
    <span></span>
    <script>
        $('button').click(function(){
            return "Welcome to TP";
        });
        $('button').click(function(event){
            $('span').text("Eevnt result: " + event.result);
        })
    </script>
</body>
</html>

輸出

執行以上程式後,它會顯示一個按鈕,當單擊它時,事件結果將顯示在其旁邊:


單擊按鈕時:


示例 3

如果上一個事件處理程式沒有返回值,則event.result 將為undefined

<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
</head>
<body>
    <p>Click on the below button to see the event result</p>
    <button>Click</button>
    <span></span>
    <script>
        $('button').click(function(){
            return;
        });
        $('button').click(function(event){
            $('span').text("Eevnt result: " + event.result);
        })
    </script>
</body>
</html>

輸出

以上程式返回 undefined 作為結果:


jquery_ref_events.htm
廣告

© . All rights reserved.