当前位置:首页 > JavaScript > 正文内容

javascript IE与FireFox 一些兼容写法

canca19年前 (2007-07-01)JavaScript435
1>获取控件用document.getElementById,不用document.all(FF等浏览器不支持)
2><button> 会被firefox解释为提交form或者刷新页面,需要写标准<button type="button">
3>使用childNode()代替之前的children 
4>获取自己定义的属性 用  document.getElementByID("TD1").getAttribute("isOBJ")  代替document.getElementByID("TD1").isOBJ
6>事件追加方法attachEvent(IE)/detachEvent;addEventListener( Mozilla, Netscape, Firefox)/removeEventListener
  又或者直接用obj.onmouseover=func;
7>Firefox中不存在 Event时间,必须通过object本身去取
   在Firefox获取当前物件的坐标方法:
  document.onmousemove = Inti_move;
  function  Inti_move(ert)
{
  x=ert.pageX;
}
表4 Mozilla与IE之间的事件属性差异
Internet Explorer Name Mozilla Name Description
altKey altKey Boolean property that returns whether the alt key was pressed during the event.
cancelBubble stopPropagation() Used to stop the event from bubbling farther up the tree.
clientX clientX The X coordinate of the event, in relation to the element viewport.
clientY clientY The Y coordinate of the event, in relation to the element viewport.
ctrlKey ctrlKey Boolean property that returns whether the Ctrl key was pressed during the event.
fromElement relatedTarget For mouse events, this is the element from which the mouse moved away.
keyCode keyCode For keyboard events, this is a number representing the key that was pressed. It is 0 for mouse events.
returnValue preventDefault() Used to prevent the event's default action from occurring.
screenX screenX The X coordinate of the event, in relation to the screen.
screenY screenY The Y coordinate of the event, in relation to the screen.
shiftKey shiftKey Boolean property that returns whether the Shift key was pressed during the event.
srcElement target The element to which the event was originally dispatched.
toElement currentTarget For mouse events, this is the element to which the mouse moved.
type type Returns the name of the event.

8>event.keyCode的問題解決
在FF中不存在window.event.keyCode屬性。可以用以下方法解決

function keepKeyNum(evt)
{
        
var  k=window.event?evt.keyCode:evt.which;
        
if ((k<=57&& (k>=48))
                
{return true;}
        
else 
                
{return false;}
}
 <input type="text" onKeyPress=" return keepKeyNum(event);" >

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

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

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

标签: JavaScript
分享给朋友:

“javascript IE与FireFox 一些兼容写法” 的相关文章

JavaScript 函数原型对象

    以下是一篇本人在Qzone里写下的文章,现在放到这里来,欠丑了。希望对一些初学者有一点点帮助。     今天给大家说说JavaScript中的类。类?没错。JavaScript中的函数原型对象就是OOP中人们熟悉的类。JS...

javascript 中面向对象編程 (类的继承)

// 人的基類var Person=new ( function(){  var sex;  var name;  this.getSex=function() &nbs...

javascript 弹出式窗体详解

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

javascript 中面向对象编程 (类的构造)

javascript 中面向对象编程 (类的构造)

不論是用java,還是c#,又或是vb,構建一個對象都很簡單,都可以采用 classobj =new classobj()的方法構造一個類,然后使用其中的屬性以及方法,其實javascript也是一樣可以實現的。    示例:建立一個js文件,定義一個對象 E...

javascrip 事件追加方法

基本方法:attachEvent(IE)/detachEvent;addEventListener( Mozilla, Netscape, Firefox)/removeEventListener在之前的邏輯判斷式的基礎上,在設計javascript的時候,可以針對瀏覽器的不同,寫出適合不同種類瀏覽...

发表评论

访客

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