我希望人们在我的网站上查看最新评论,当他们点击它时,
它应该将他们带到撰写评论的业务页面,但该页面也应该向下滚动到评论所在的位置。
我在想,如果我给每个 div 指定一个不同的类名,并在类名中包含评论 ID,这可能是可能的吗? (我需要给它们相同的 id 来应用 css 格式)。
更具体地说,如何使用 jquery 直接进入 DOM review_XXX(xxx 代表评论 ID)
最佳答案
为此您不需要 jQuery。只需为链接提供一个带有 #
前缀的 href,后跟要跳转到的元素的 ID,例如:
<a href="#foo">Foo</a>
...
<div id="foo">This is the div to jump to</div>
参见"anchors with the ID attribute" in the W3C HTML Specification.
上述网站的有趣摘录:
Use id or name? Authors should consider the following issues when deciding whether to use id or name for an anchor name:
- The id attribute can act as more than just an anchor name (e.g., style sheet selector, processing
identifier, etc.).- Some older user agents don't support anchors created with the id attribute.
- The name attribute allows richer anchor names (with entities).
另请参阅(针对 HTML5):
关于jquery - 如何从链接直接浏览到特定评论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5539457/