问题
Consider the following code. If I click in cmbMonkeys, it causes an infinite loop of alert messages in Google Chrome. My workaround for cmbPeople works fine. Does anyone know of a another way to prevent infinite loops when displaying alerts on focus or blur events, without disabling and re-enabling the event?
<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:
With a simple state-handler, maybe?
var isFocus = false;
function cmbPeople_CallFocusHandler(control) {
if(!focus){
focus = true;
cmbPeople_OnFocusHandler();
}
}
function cmbPeople_CallBlurHandler(){
isFocus = false;
}
来源:https://stackoverflow.com/questions/6151027/can-one-prevent-chrome-causing-an-infinite-loop-when-displaying-alerts-on-the-fo