更改URL而不使用<a href="">
change URL without reloading the page with <a href=" ">
对于在不重新加载页面的情况下修改URL,我们可以使用:
window.history.pushState('page2', 'Title', '/page2.php');
但如果我尝试使用<a>
元素:
<a onClick='my_function()' href='/page2.php'> Click Here </a>
<script>
function my_fnuction(){
window.history.pushState('page2', 'Title', '/page2.php');
}
</script>
它不起作用,那么我如何将<a>
元素和window.history.pushState()
一起使用呢?与twitter和Facebook一样,用户可以点击右键在新窗口中打开URL,并直接点击获取页面和更改URL,而无需重新加载
-
在onclick中,您有
my_function
,其中实际的函数名称是my_fnuction
。 -
您需要取消默认行为,如果onclick函数返回false,则取消默认浏览器行为。
代码:
<a onClick='return my_function()' href='/page2.php'> Click Here </a>
<script>
function my_function(){
window.history.pushState('page2', 'Title', '/page2.php');
return false;
}
</script>
添加return false;
使其不执行href
,也将return
添加到您的onclick
值:
<a onClick='return my_function()' href='/page2.php'> Click Here </a>
<script>
function my_function(){
window.history.pushState('page2', 'Title', '/page2.php');
return false;
}
</script>
解决方案也在这里解释
可能只是阻止了锚点标记的默认行为。事件返回false
<a onClick='return my_function()' href='/page2.php'> Click Here </a>
<script>
function my_function(){
window.history.pushState('page2', 'Title', '/page2.php');
return false;
}
</script>
或者使用防止默认:
<a onClick='my_function(event); ' href='/page2.php'> Click Here </a>
<script>
function my_function(e){
window.history.pushState('page2', 'Title', '/page2.php');
e.preventDefault();
}
</script>
相关文章:
- 检测电话窃听,即:<a href="电话:xxx">在UIWebview上
- 如何提取“;href"最近列表项中的属性值
- CKEditor如何允许href="javascript:void(0)"在小部件中
- 如何使用Javascript获取当前页面URL,并在<a href="[the_URL]”>
- JSP<a href="">无法保存常规语句
- jQuery:使用.attr('href',")调用javascript
- Linkify/Clickable Text URL,但忽略那些已经包装在“;a href"'s
- 阻止href="#"链接更改URL哈希
- a href="上的javascript对象对象;javascript:"呼叫
- "href"JavaScript Android中的链接不起作用
- 菜单href="#pannel”;不适用于android
- 参考“;href"而不是html中锚点标签中的url
- 更改URL而不使用<a href="">
- 冷融合:空链接href="#"错误
- 如何检查带有href="某事“;与Jquery有一定的类
- 突出显示菜单中具有href="#"“选项”;a“;标签
- jQuery替换所有href=""其中onclick=“;window.location=“;
- <a href="#">最终调用Javascript函数
- 命令“;window.location.href"不适用于HTML(引导框架)
- href对页面的HTML调用"UP”;目录