Home  >  Article  >  Web Front-end  >  Summary of how a link executes js (with code)

Summary of how a link executes js (with code)

不言
不言Original
2018-08-23 17:04:412349browse

The content of this article is a summary of how to execute js on a link (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Purpose: prohibit jumps and directly execute the function bound to a link

href="javascript:;", where javascript: is a pseudo-protocol, which allows We call the javascript function through a link. In this way, javascript:; can realize the click event of the A tag when running. If the page has a lot of content and there is a scroll bar, the page will not jump around and the user experience will be better

1: Direct execution

<a href="javascript:a()&#39;;">  //直接执行函数a不推荐

2: No jump execution recommendation

<a href="javascript:void(0);" onclick=&#39;a()&#39;>   //不发生跳转直接执行函数a 推荐

3: Same as above Recommendation

<a href="javascript:;" onclick=&#39;a()&#39;>       //执行了一条空的js代码 推荐

4: Empty anchor point

<a href="#" onclick=&#39;a()&#39;>   
//#表示top用这种方法点击后网页后返回到页面的最顶端。

5: Same as above, do not return to the top of the page Recommended

<a href="#" onclick=&#39;a()&#39; return false;>    //触发事件但不跳转  推荐

Related recommendations:


The above is the detailed content of Summary of how a link executes js (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn