SoFunction
Updated on 2025-03-09

Solution to JavaScript fail to set focus using focus

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

("cancel").focus()

But the hard thing is that the set-top box is really a big pit. Since it is compatible with all existing set-top box models, 8 set-top boxes need to be adapted. Then a problem arose! A set-top box of ZTE B600 cannot set the focus to the Cancel button at all. Here are my solutions:

First, confirm whether the set-top box supports the getElementById() method and whether it successfully obtained the element with the ID "cancel": the test method is very simple. I directly wrote another <p >test</p>, and then obtained the element with the ID "test" in the same place, and performed a simple operation ("test").innerHTML="Hello"
Finally, I used "try...catch(e)..." to catch the reason why "focus()" failed

try(){ 
<span style="white-space:pre"> </span>("cancel").focus() 
}catch(e){ 
<span style="white-space:pre"> </span>alert( + ": " + ()); 
}

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

After more than an hour of struggle, the big boss finally appeared, and he solved the problem with just a simple sentence! It is possible to actively call flur() to cancel the original focus!

("purchase").flur()

Then the problem was solved. I have to express my feelings! In the process of solving this problem, my own ideas are actually quite correct, but my knowledge is obviously not enough. The gap between general programmers and senior programmers is not only about solving problems, but also about experience and knowledge!