-1

我有两个div要素:

<div id="one">
    <a href="#two">Link to two</a>
</div>
<div id="two">
    Link to me
</div>

当我单击href属性为的链接时,这会导致以下情况#two

http://domain.com/one#two

但我想要:

http://domain.com/#two

这可能不使用javascript吗?

4

1 回答 1

1

解决方案

考虑到您页面的奇怪行为,不,没有 JavaScript 是不可能的

click链接事件中,转到指定的位置href

Javascript

window.onload = function(){
    /* On window load event, we loop through each link */
    var a  = document.getElementsByTagName('a');
    for (var elem in a)
    {
        /* If it's a DOMElement */
        if(a[elem].nodeType ==1)
        {
            /* We bind the window.location.href event, sending it to the value of the href attribute of the selected anchor element. */

            a[elem].addEventListener("click",function(e){window.location.href = this.getAttribute('href');alert(window.location.href);});

            /* console.log(window.location.href); */
        }
    }
}

-->现场演示<--

于 2013-06-21T14:02:40.223 回答