Is it possible to make an href jump on the same page with the destination element not having an id?

拈花ヽ惹草 提交于 2019-12-25 09:28:15

问题


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>

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/44280669/is-it-possible-to-make-an-href-jump-on-the-same-page-with-the-destination-elemen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!