我想在新标签页中打开一个链接,然后转到该链接中的特定div。链接是:
我想点击链接转到div #paper-window
。这是我尝试过的:
1
<a href="http://paper.li/f-1333760510#paper-window" target="_blank" />
2
<a href="#" onclick="load-link(); return false" />
function load-link() {
var w = window.open("http://paper.li/f-1333760510");
w.location.href = "http://paper.li/f-1333760510#paper-window";
}
答案 0 :(得分:1)
链接看起来没问题...但是您错误地将location.hash放入元素的ID ...
OLD: 您的锚标记绝对可以:
<a href="http://paper.li/f-1333760510#paper-window" target="_blank">open me</a>
<a name="paper-window"></a>
因为网址的#something
部分是该网页上某个锚点的跳转点,而不是指向ID
更新(关于你的评论,感谢各位:)) Navigating to a fragment identifier的HTML5规范确实说两者都可以工作......顺序是首先查找一个id,然后是一个带有属性名称的锚(尽管在HTML5中已弃用以实现向后兼容)
所以这意味着打开http://www.example.com#foo会导致搜索:
// check for an element with an id matching "foo"
<div id="foo"></div>
// if no element was found check for an anchor with that name (also backward compatibility)
<a name="foo"></a>
// compare location.hash with "top"
// would jump to the top of the page if it matches