简体   繁体   中英

Input doesn't focus in Opera 54.0

I am having an input that I try to focus on click event, the problem seems to appear only in Opera and I think is because I am having an alert before the call of the focus.

My code is here (and in this fiddle ):

 function Validate(){ var element=document.getElementById('test-input'); if(element.value=="") { alert("Please complete the field!"); element.focus(); } } 
 <input id="test-input"/> <input type="button" value="Click" onClick="Validate();"/> 

It actually works in Opera 55.0, but it just doesn't blink the cursor. If you begin typing immediately after the message it begin with the field with the focus. You could add some styling to the field, red border around it or some other visual effect, to accommodate that.

--- update ---

It's possible that working on the field, like I did in that fiddle that has now disappeared, after finishing the dialog that it increase Opera's awareness of the focus:

Like this:

function Validate(){
   var element=document.getElementById('test-input');
   if(element.value=="")
   {
      window.alert("Please complete the field!");
      element.style.border = "solid 1px red";
      element.focus();
   } else {
      element.style.border = "";
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM