首页
学习
活动
专区
工具
TVP
发布

java大数据

专栏作者
627
文章
445360
阅读量
29
订阅数
javascript当中绑定事件和方法
8.绑定事件和方法 once, long time to know that "script" must be put in behind, while "input" must be put in front, otherwise document.getElementById("button1"); can not find the "button1",alert("button1 is "+ button1); 结果就是null,为什么这次跟往常我们的印象不一样了呢?因为往常我们先写一段script,之后在body中写上诸如<INPUT TYPE="button" onclick="abc",之类的话,这样上面的abc这样的代码开始不会被执行,里面的诸如document.getElementById ("button1");也就正确了。这里为什么跟往常不一样呢?因为要在一开始时,先给button绑上事件代码,否则button无事件响应。 例 8.1(commonEventPrerequisiteIEFF.html) <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> </head> <INPUT TYPE="button" NAME="button1" ID="button1" VALUE="单击我"/> <script> function testie() { alert("单击按钮ie"); } function testFF() { alert("单击按钮FireFox"); } /**/ /*obj.addEventListener("click",function(){func("看到我了吧");},false); the third argument is: A Boolean flag value that indicates whether the event listener should use bubbling (由里向外) or capture (由外向里) event propagation. 只要我们 知道第三个参数和事件冒泡有关就可以了。缺省值为假,即冒泡的意思。具体例子参考后面的事件冒泡例子。 */ var button1 = window.document.getElementById("button1"); alert("button1 is "+ button1); alert("document.all is" + document.all); alert(typeof(window.addEventListener) + " is typeof window.addEventListener"); alert(typeof(window.attachEvent) + " is typeof window.attachEvent"); alert(window.addEventListener + " is window.addEventListener"); alert(window.attachEvent + " is window.attachEvent"); if (typeof window.attachEvent === "object") { alert("ie"); button1.attachEvent("onclick", testie); } if (typeof window.addEventListener === "function") { alert("firefox"); button1.addEventListener("click", testFF, false); } // button1.addEventListener("click",test,false); //button1.attachEvent("onclick" , test); var str = ""; </script> 更多请见:https://blog.csdn.net/qq_43650923/article/details/102210095
马克java社区
2019-10-29
6550
javascript当中抑制原始功能用法
7.抑制原始功能 例 7.1(PreventDefaulIEFF.html) <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> <a id="kk" href="http://www.abc.com">淘宝</a>. <script> function Clicks(event) { var event = event || window.event; if (window.navigator.userAgent.indexOf("MSIE") >= 1) { event.returnValue = false; } else if ( window.navigator.userAgent.indexOf("Firefox") >= 1) { event.preventDefault(); } alert("链接被click"); } if (window.navigator.userAgent.indexOf("MSIE") >= 1) { document.getElementById("kk").attachEvent("onclick" , Clicks); } else if (window.navigator.userAgent.indexOf("Firefox") >= 1) { /*obj.addEventListener("click",function(){func("看到我了吧");},false); the third argument is: A Boolean flag value that indicates whether the event listener should use bubbling (由里向外) or capture (由外向里) event propagation. 只要我们 知道第三个参数和事件冒泡有关就可以了。缺省值为假,即冒泡的意思。具体例子参考后面的事件冒泡例子。 */ document.getElementById("kk").addEventListener("click", Clicks, true); } </script>
马克java社区
2019-10-21
3860
没有更多了
社区活动
RAG七天入门训练营
鹅厂大牛手把手带你上手实战
Python精品学习库
代码在线跑,知识轻松学
博客搬家 | 分享价值百万资源包
自行/邀约他人一键搬运博客,速成社区影响力并领取好礼
技术创作特训营·精选知识专栏
往期视频·千货材料·成员作品 最新动态
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档