javascript IE與FireFox 一些相容寫法 (小技巧七)

來源:互聯網
上載者:User
1>擷取控制項用document.getElementById,不用document.all(FF等瀏覽器不支援)
2><button> 會被firefox解釋為提交form或者重新整理頁面,需要寫標準<button type="button">
3>使用childNode()代替之前的children 
4> 手型滑鼠指標請用cursor:pointer,不用 cursor:hand
5>擷取自己定義的屬性 用  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);" >
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.