IE和Firefox的相容性問題

來源:互聯網
上載者:User

1.event.srcElement問題

在IE下,event對象有srcElement屬性,但是沒有target屬性;
在Firefox下,even對象有target屬性,但是沒有srcElement屬性.

我們可以這樣解決:

網頁教學網


使用obj(obj = event.srcElement ? event.srcElement : event.target;)來代替IE下的event.srcElement或者Firefox下的event.target.

2.上面提到了event,在兩種瀏覽器中也存在問題
window.event只能在IE下運行,而不能在Firefox下運行,
這是因為Firefox的event只能在有事件發生的情況下使用

網頁教學網


IE:
<input type="button" value="測試IE下的Event" onclick="test1()"/>
<input type="button" value="測試Event的相容性" onclick="test2()"/>

Webjx.Com


<script language="javascript">
function test1() {
alert(window.event); //使用 window.event
}
function test2(evt) {
evt=evt?evt:(window.event?window.event:null);
alert(evt); //使用參數evt
}
</script>
=============================================
3.innerText在IE中能正常工作,但是innerText在FireFox中卻不行
這裡給出一個解決方案
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>關注一下IE和Firefox的相容性</title>
</head>
<script language="javascript">
function testInnerText(){
if(navigator.appName.indexOf("Explorer") > -1){
document.getElementById('element').innerText = "my text";
} else{
document.getElementById('element').textContent = "my text";
}
}
</script>
<body>
<input name="Button" type="button" value="testInnerText" onclick="javascript:testInnerText()"/>

網頁教學網

<div id="element" style="border:1px solid #0066CC; width:100px; height:50px"></div>
</body>
</html> Webjx.Com

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.