请考虑以下代码。如果我点击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>
答案 0 :(得分:4)
使用简单的状态处理程序,可能吗?
var isFocus = false;
function cmbPeople_CallFocusHandler(control) {
if(!focus){
focus = true;
cmbPeople_OnFocusHandler();
}
}
function cmbPeople_CallBlurHandler(){
isFocus = false;
}