最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get sent to an class by URL - Stack Overflow

programmeradmin1浏览0评论

I know this is a little bit hard to understand, but I've seen some sites using the ID of an element in the url to send you somewhere, and I was wondering if it's also possible with classes.

I don't think this is private or something like that because anyone can get it by inspecting the page. I also tryed with a dot (".class"), but it unfortunately didn't work.

So is there another way of doing this with other than IDs?

I know this is a little bit hard to understand, but I've seen some sites using the ID of an element in the url to send you somewhere, and I was wondering if it's also possible with classes.

I don't think this is private or something like that because anyone can get it by inspecting the page. I also tryed with a dot (".class"), but it unfortunately didn't work.

So is there another way of doing this with other than IDs?

Share Improve this question edited Feb 11, 2021 at 18:52 asked Aug 30, 2016 at 20:24 user6312124user6312124 4
  • <a href="#div">Content</a> – Arnav Borborah Commented Aug 30, 2016 at 20:26
  • clicking on a link with a hashtag will scroll you to the point on that page where that id occurs. it's called a bookmark. if you used classes how would the browser know which one to scroll to? why do you want to do that anyway? – Robert Parham Commented Aug 30, 2016 at 20:29
  • Hashes can only refer to id attributes and name attributes, but not class or any other attribute. – mwcz Commented Aug 30, 2016 at 20:29
  • 1 Wrong answer Arnav. But anyway, I don't think there's anything that can be done with classes as there is no guarantee a class is unique, therefore how does the browser know which to scroll to? Could implement some behavour with JavaScript which takes the first – Joseph Young Commented Aug 30, 2016 at 20:29
Add a ment  | 

1 Answer 1

Reset to default 8

You cannot do this with classes because they're not unique. The browser needs to know exactly where to send you, and using a class name instead of an ID would be ambiguous.

发布评论

评论列表(0)

  1. 暂无评论