我有两个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吗?
答案 0 :(得分:1)
考虑到您网页的奇怪行为,不,没有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); */
}
}
}