Is it possible to make an href jump on the same pa

2019-09-15 03:27发布

Is it possible to make an <a href> link to the same page when your destination doesn't have an ID?

<a href = "#">Jump to Topic 1</a>
...
<!--destination is below-->
<h1 class="tab-title" data-editor-style="title" style="color:#444444">Topic 1</h1>

2条回答
Rolldiameter
2楼-- · 2019-09-15 04:03

A URL fragment identifier can only target an element with a matching ID, or a corresponding named anchor. You cannot link to arbitrary elements without IDs. (There was a proposal that would have allowed the use of CSS selectors as fragment identifiers, in which I was personally involved, but it never took off.)

The best you can do is to use JavaScript to select the element you want and give it an ID that is reserved for your hyperlink. If the elements matching this selector will change then you will additionally have to listen for such changes and handle them accordingly, but that's probably beyond the scope of this question.

查看更多
The star\"
3楼-- · 2019-09-15 04:04

Sadly, the answer is no. You need an ID or at least a name, like CBroe mentioned in a comment of your question.

However, I found this here:

Today, all browsers recognize top as being the top of the page. If you want the visitor to go to the very top of the page, the ID tag can be left out and the A HREF link will still work.

查看更多
登录 后发表回答