通过javascript模拟单击锚标记

我试图通过javascript模拟对锚标记的点击来更改浏览器地址框中的文本.此代码在IE中运行良好,但在FireFox或Chrome中不行.

<script type="text/javascript">
    function UpdateQueryString() {
        var controlRef = document.createElement('a');
        controlRef.id = "t1";
        controlRef.href = '#1';
        controlRef.innerHTML = '';
        document.body.appendChild(controlRef);

        try {

        controlRef.click();
            }
        catch (err) {
            txt = "There was an error on this page.\n\n";
            txt += "Error description: " + err.description + "\n\n";
            txt += "Click OK to continue.\n\n";
            alert(txt);
        }
        return false;
    }
</script>

在IE以外的浏览器中,我可以找到锚标记,但调用单击会导致错误.

谢谢

解决方法:

你在寻找location property

location = '#1';

要么

location.hash = '#1';
上一篇:单击使用htmlunit的javascript锚点似乎不起作用


下一篇:javascript – Anchor Onclick Division亮点