当前位置:首页 > JavaScript

javascrip 事件追加方法

canca19年前 (2007-07-01)JavaScript462

基本方法:attachEvent(IE)/detachEvent;addEventListener( Mozilla, Netscape, Firefox)/removeEventListener

在之前的邏輯判斷式的基礎上,在設計javascript的時候,可以針對瀏覽器的不同,寫出適合不同種類瀏覽器的代碼。
追加代碼:

var obj=document.getElementById("id1")
if(window.addEventListenner)// Mozilla, Netscape, Firefox
 obj.addEventListenner("click",clickfun,false)
else
 obj.attachEvent(
"onclick",clickfun)


移除代碼:

if(window.removeEventListener())
// Mozilla, Netscape, Firefox 
   daycol.removeEventListener('click', clickfun, false); 
}
 
else
 
// IE
 daycol.detachEvent('onclick', clickfun); 
}


function clickfun(value)
{
  alert(value)
}

3>另外一種簡單的方法
document.getElementById("txtvalue2").onclick=function(evt){alert(evt); }

扫描二维码推送至手机访问。

版权声明:本文由Ant.Master's Blog发布,如需转载请注明出处。

本文链接:https://iant.work/post/584.html

标签: JavaScript
分享给朋友:

“javascrip 事件追加方法” 的相关文章

javascript MailTo 邮件技巧

调用email的方法 //<a href="mailto:talantlee@126.com">Email</a>window.location.href="mailto:talantlee@126.com";myform.action="mailto:talant…

javascript IE与FireFox 一些兼容写法

1>获取控件用document.getElementById,不用document.all(FF等浏览器不支持)2><button> 会被firefox解释为提交form或者刷新页面,需要写标准<button type="button">3>使用childN…

javascript 弹出式窗体详解

1>window.prompt(text, value) 簡單的基與模態窗體的對話框,(返回你輸入)   var v=window.prompt("提示","請輸入你的名字")2>window.confirm(text,mess)  模態確認框(返回"是/否…

event事件兼容mozilla firefox的问题

本来一个写好了的脚本在IE下正常,可是在mozilla firefox中我们发现程序报类似如下的错误: event is not defined obj has no properties 原因是Firefox中使用了不同的事件对象模型,不同于IE Dom,用的是W3C Dom. 下是我在网上找到的…

JavaScript 为网页添加快捷键

为网页添加快捷键即为网页添加onkeydown事件。 FireFox里获取键入键的ASCII码与IE获取是有些差别的。因为FF里没有Event对象。 但FF里事件触发方法中默认传送其事件对象。 即我们在JS里写该事件方法时,要多加一个参数。 例: <sc...…

JavaScript对粘贴板的全操作

<html>    <head>        <script language="javascript">function copy_clip(copyObjectName…

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。