在HTML中引用父div的兄弟姐妹

时间:2013-06-21 13:46:59

标签: html parent siblings

我有两个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吗?

1 个答案:

答案 0 :(得分: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); */
        }
    }
}

-->Live Demo<--