Home > Web Front-end > JS Tutorial > Solution to JavaScript failure to use focus() to set focus_javascript tips

Solution to JavaScript failure to use focus() to set focus_javascript tips

WBOY
Release: 2016-05-16 16:37:24
Original
2036 people have browsed it

Yesterday when I modified the EPG page on the set-top box, I encountered a small problem. When users purchase a game, a purchase confirmation dialog box needs to pop up. The default focus of the dialog box must stay on the "Cancel" button. A very simple requirement can be achieved using JavaScript's focus() method. A simple code example is as follows:

document.getElementById("cancel").focus()
Copy after login

But the sad thing is that the set-top box is really a big pit. Since it needs to be compatible with all existing set-top box models, 8 sets-top boxes need to be adapted. Then something went wrong! A ZTE B600 set-top box is completely unable to set the focus to the cancel button. The following is my solution:

First confirm whether the set-top box supports the getElementById() method and whether the element with the ID "cancel" has been successfully obtained: the test method is very simple, just write another

test Finally, "try...catch(e)..." was used to capture the reason why "focus()" failed

try(){ 
<span style="white-space:pre"> </span>document.getElementById("cancel").focus() 
}catch(e){ 
<span style="white-space:pre"> </span>alert(e.name + ": " + e.what()); 
}
Copy after login

But it’s just weird! The results of the above two steps show that the set-top box supports focus() and getElementById(), but the focus cannot be set to the pop-up dialog box.

After struggling for more than an hour, the big BOSS finally appeared and solved the problem with just one sentence! It is possible to actively call flur() to cancel the original focus!

document.getElementById("purchase").flur()
Copy after login

Then the problem is solved. I have to appreciate it! In the process of solving this problem, my idea was actually quite correct, but my knowledge was obviously not enough. The gap between ordinary programmers and senior programmers is not only in terms of ideas for solving problems, but also in experience and knowledge!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template