How to Use focus () to set focus failure in JavaScript _ javascript skills

Source: Internet
Author: User
This article mainly introduces a solution to the failure of setting the focus using focus () in JavaScript. If you need it, you can refer to the EPG page on the top of the set-top box modified yesterday and encounter a small problem. When purchasing a game, you need to bring up the purchase confirmation dialog box. The default focus of the dialog box must be on the "cancel" button. You can simply use the JavaScript focus () method. A simple code example is as follows:

document.getElementById("cancel").focus()

However, it is hard to say that the set-top box is really a big pitfall. To be compatible with all existing set-top boxes, you need to adapt the eight set-top boxes. Then there is a problem! A set-top box of ZTE B600 cannot set the focus to the cancel button. Here are my solutions:

First, check whether the set-top box supports the getElementById () method and whether the element whose ID is "cancel" is successfully obtained. The test method is very simple.

Test

And then obtain the element with ID "test" in the same place, and perform a simple operation for document. getElementById ("test"). innerHTML = "Hello"
Finally, we used "try... catch (e)..." to capture the cause of "focus ()" failure.

try(){  document.getElementById("cancel").focus() }catch(e){  alert(e.name + ": " + e.what()); }

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

I spent more than an hour in the past and later, and finally the big BOSS came into being. I simply said a word to solve this problem! You may need to call flur () to cancel the original focus!

document.getElementById("purchase").flur()

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.