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()';"> //直接执行函数a不推荐
2: No jump execution recommendation
<a href="javascript:void(0);" onclick='a()'> //不发生跳转直接执行函数a 推荐
3: Same as above Recommendation
<a href="javascript:;" onclick='a()'> //执行了一条空的js代码 推荐
4: Empty anchor point
<a href="#" onclick='a()'> //#表示top用这种方法点击后网页后返回到页面的最顶端。
5: Same as above, do not return to the top of the page Recommended
<a href="#" onclick='a()' 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!