可以在显示焦点事件的警报时阻止Chrome导致无限循环,而不会禁用和重新启用事件吗?

时间:2011-05-27 10:35:07

标签: javascript html google-chrome javascript-events

请考虑以下代码。如果我点击cmbMonkeys,它会在Google Chrome中导致无限循环的提醒消息。我对cmbPeople的解决方法很好。有没有人知道在焦点或模糊事件上显示警报时防止无限循环的另一种方法,而不禁用和重新启用事件?

<html>
<head>
    <script>
        var eventHandler;

        function cmbPeople_OnFocusHandler() {
            alert("focus");
        }

        function cmbPeople_CallFocusHandler(control) {
            eventHandler = control.onfocus;
            control.onfocus = null;
            cmbPeople_OnFocusHandler();
        }

        function cmbPeople_CallBlurHandler(control) {
            control.onfocus = eventHandler;
        }

        function cmbMonkeys_FocusHandler(control) {
            alert("I like monkeys");
        }
    </script>
</head>
<body>
    monkeys <select id="cmbMonkeys" onfocus="cmbMonkeys_FocusHandler(this)"></select>
    people <select id="cmbPeople" onfocus="cmbPeople_CallFocusHandler(this)" onblur="cmbPeople_CallBlurHandler(this)"></select>
</body>
</html>

1 个答案:

答案 0 :(得分:4)

使用简单的状态处理程序,可能吗?

var isFocus = false;
function cmbPeople_CallFocusHandler(control) {
    if(!focus){
        focus = true;
        cmbPeople_OnFocusHandler();
    }
}

function cmbPeople_CallBlurHandler(){
    isFocus = false;
}