5阻止A默认行为和JS实现页面跳转的方法

时间:2023-01-16 21:08:22
<!--HTML中阻止A标签的默认行为: href="javascript:;" href="javascript:void 0;"-->
<!--<a href="javascript:;" class="link" id="link">购物</a>-->
阻止a的默认行为的两种方式:
<a href="" class="link" id="link">购物</a>
<script type="text/javascript">
//->JS阻止A的默认行为的两种方式 /*document.getElementById("link").onclick = function () {
return false;
};*/ document.getElementById("link").onclick = function (e) {
e = e || window.event;
e.preventDefault ? e.preventDefault() : e.returnValue = false;
};
</script>

JS中实现页面跳转的方法:

<div id="box"></div>
<script type="text/javascript">
//JS中实现页面跳转的方法
document.getElementById("box").onclick = function () {
//window.location.href = "http://www.zhufengpeixun.cn/";//->在当前页面实现跳转
//console.log(window.location.href); //->获取当前页面地址栏中的URL地址
//window.location.href = window.location.href; ->刷新本页面 //window.open("http://www.zhufengpeixun.cn/"); //->在新的窗口打开页面
};
</script>