首页技术onclick?click

onclick?click

编程之家2026-06-04989次浏览

大家好,onclick相信很多的网友都不是很明白,包括click也是一样,不过没有关系,接下来就来为大家分享关于onclick和click的一些知识点,大家可以关注收藏,免得下次来找不到哦,下面我们开始吧!

onclick?click

详解a标签添加onclick事件的几种方式

我们常用的在a标签中有点击事件:

1. a rel="external nofollow" href="javascript:js_method();" rel="external nofollow"

这种方法在传递this等参数的时候很容易出问题,而且javascript:协议作为a的href属性的时候不仅会导致不必要的触发window.onbeforeunload事件,在IE里面更会使gif动画图片停止播放。W3C标准不推荐在href里面执行 javascript语句

2. a rel="external nofollow" rel="external nofollow" href="javascript:void(0);" rel="external nofollow" rel="external nofollow" onclick="js_method()"

这种方法是很多网站最常用的方法,也是最周全的方法,onclick方法负责执行js函数,而void是一个操作符,void(0)返回undefined,地址不发生跳转。而且这种方法不会像第一种方法一样直接将js方法暴露在浏览器的状态栏。

3.a rel="external nofollow" rel="external nofollow" href="javascript:;" rel="external nofollow" rel="external nofollow" onclick="js_method()"

onclick?click

这种方法跟跟2种类似,区别只是执行了一条空的js代码。

4.a rel="external nofollow" rel="external nofollow" rel="external nofollow" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="js_method()"

这种方法也是网上很常见的代码,#是标签内置的一个方法,代表top的作用。所以用这种方法点击后网页后返回到页面的最顶端。

5.a rel="external nofollow" rel="external nofollow" rel="external nofollow" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="js_method();return false;"

这种方法点击执行了js函数后return false,页面不发生跳转,执行后还是在页面的当前位置。

综合上述,在a中调用js函数最适当的方法推荐使用:

onclick?click

a rel="external nofollow" rel="external nofollow" href="javascript:void(0);" rel="external nofollow" rel="external nofollow" onclick="js_method()"

a rel="external nofollow" rel="external nofollow" href="javascript:;" rel="external nofollow" rel="external nofollow" onclick="js_method()"

a rel="external nofollow" rel="external nofollow" rel="external nofollow" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="js_method();return false;"

以上所述是小编给大家介绍的a标签添加onclick事件的几种方式详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

addEventListener和普通.onclick的区别

addEventListener是在 FireFox上的用法。

addEventListener的参数一共有三个,语法为:

element.addEventListener(type,listener,useCapture)

详解

其中element是要绑定函数的对象。

type是事件名称,要注意的是"onclick"要改为"click","onblur"要改为"blur",也就是说事件名不要带"on"。

listener当然就是绑定的函数了,记住不要跟括号

最后一个参数是个布尔值,表示该事件的响应顺序,下面重点介绍一下addEventListener的第3个参数(useCapture)。

userCapture若为true,则浏览器采用Capture,若为false则采用bubbing方式。建议用false

区别:

varbtn1Obj=document.getElementById("btn1");

//element.addEventListener(type,listener,useCapture);

btn1Obj.addEventListener("click",method1,false);

btn1Obj.addEventListener("click",method2,false);

btn1Obj.addEventListener("click",method3,false);执行顺序为method1->method2->method3

document.getElementById("btn").onclick=method1;

document.getElementById("btn").onclick=method2;

document.getElementById("btn").onclick=method3;如果这样写,那么将会只有medhot3被执行

a标签的href和onclick 的事件的区别介绍

以前一直很随意,后来看.net里的linkbutton似乎是用在<a rel="external nofollow" href="javascript:fun();"...>的形式,今天用这种方式就遇到一些问题,摘网友的文章和我的结论放在下面:

1.链接的 onclick事件被先执行,其次是 href属性下的动作(页面跳转,或 javascript伪链接);

2.假设链接中同时存在 href与 onclick,如果想让 href属性下的动作不执行,onclick必须得到一个 false的返回值。不信,你可以将 goGoogle函数中的 return false注释掉;

3.如果页面过长有滚动条,且希望通过链接的 onclick事件执行操作。应将它的 href属性设为 javascript:void(0);,而不要是#,这可以防止不必要的页面跳动;

4.如果在链接的 href属性中调用一个有返回值的函数,当前页面的内容将被此函数的返回值代替;

5.在按住Shift键的情况下会有所区别。

6.今天我遇到的问题,在IE6.0里以href的形式访问不到parentNode。

7.尽量不要用javascript:协议做为A的href属性,这样不仅会导致不必要的触发window.onbeforeunload事件,在IE里面更会使gif动画图片停止播放。

就这些,花了不少时间在这上面。

关于onclick到此分享完毕,希望能帮助到您。

css样式表文件 css外部样式表怎么写java开发的软件有哪些(java开发app的流程)