【JS】实现 a 标签点击不跳转

一、利用空代码

  • 这种方法跟 二 类似,区别只是执行了一条空的 js 代码。
<a href="javascript:;">不能跳到百度</a>

二、利用伪协议

  • void 是一个操作符,void(0) 返回 undefined,地址不发生跳转
<a href="javascript:void(0);" >不能跳到百度</a>

三、返回 false

  • html 写法
<a href="http://www.baidu.com" onclick="return false" >不能跳到百度</a>
  • jsx 写法
<a 
	href="http://www.baidu.com" 
	onclick={
    
    ()=>{
    
    
		console.log('函数里面可以正常执行函数操作')
		return false;
	}} 
>
不能跳到百度
</a>

猜你喜欢

转载自blog.csdn.net/qq_45677671/article/details/131012151
今日推荐