3

当我在firefox中使用window.open打开一个新窗口并尝试使用跳过导航在新窗口中导航页面的不同部分时不起作用并且预览窗口被重新加载。这就是我正在做的事情。

<html>
<head>
    <script>
    function openWindow()
        {
            var win = window.open("","_blank");
            win.document.write("<!DOCTYPE html><html><head></head><body><a href='#rajeev'>hi</a><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><div id='rajeev'>To Come Here</div></html>");
            win.document.close();
        }
    </script>
</head>
<body>
    <button type="button" onclick="openWindow()">Display Date</button>
</body>
</html>
4

1 回答 1

1

您应该能够将以下 onclick 代码添加到您的链接,而不是哈希:

onclick='$(document).scrollTop($(\"#rajeev\").offset().top);'
// Escaped the quotes since it will be placed in a document.write("");

您需要在弹出窗口的代码中导入 jQuery,但如果我只使用它:

`win.document.write("<script src='http://code.jquery.com/jquery-1.8.2.js' type='text/javascript'></script>`

浏览器似乎将该</script>标记解释为整个“弹出式书写”脚本的结束标记。这可以通过切割</script>字符串中的标签来规避,如下所示。

此代码适用于 Firefox 和 Chrome:

<html>
    <head>
        <script>
            function openWindow()  {
                var win = window.open("","_blank");
                win.document.write("<!DOCTYPE html><html><head><script src='http://code.jquery.com/jquery-1.8.2.js' type='text/javascript'></scr");
                // Split up the </script> tag.
                win.document.write("ipt></head><body><a  onclick='$(document).scrollTop($(\"#rajeev\").offset().top);'>hi</a><br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br><div id='rajeev'>To Come Here</div></html>");
                win.document.close();
            }
        </script>
    </head>
    <body>
        <button type="button" onclick="openWindow()">Display Date</button>
    </body>
</html>​

现在,您只需设置 的样式<a>,使其看起来像您可以单击它,或者将其替换为<button>,例如。

(Stackoverflow 有点弄乱了上面代码中的语法突出显示,但它是有效的 js。此外,您通常不希望在 HTML 中添加内联事件侦听器。)

于 2012-11-22T07:39:59.980 回答