简体   繁体   中英

How do I “select” a link using Javascript so enter visits it

I'm sure this is a basic question, but I just cannot find the right words to ask the almighty search-engines. I'm using the word "selected" in quotes because I don't know the right term for it. I'm sure if I did the question would be readily answered already.

When you press on most webpages, you are able to cycle through the links on the page such that when you press the browser visits the link in question. This is what a selected link looks like in Firefox:

浏览器中带有虚线边框的链接,表示它已被选中

I want to be able to do this automatically on page load, such that immediately after load takes you to the link of my choosing, but any other shenanigans like inputting in an input field cancels this behavior. I've looked through the DOM browser and can see nothing jumping out differentiates "This is a link" from other links on this page.

It is called focus and you can have only one element focused . As soon as you click to an input your focus moves to it. That's why you can't have focused link while writing in an input at the same time.

Focus determines where keyboard events go in the page at any given moment.

Source & you can read more at: Introduction to Focus | Web Fundamentals

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