我有两个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吗?
考虑到您页面的奇怪行为,不,没有 JavaScript 是不可能的。
在click
链接事件中,转到指定的位置href
:
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); */
}
}
}