各瀏覽器中javascript相容性問題

來源:互聯網
上載者:User

1、使用setAttribute()改變網頁中標籤的onclick屬性
2、IE中通過 setAttribute添加的屬性


1、使用setAttribute()改變網頁中標籤的onclick屬性 *p1

      setAttribute設定style和onclick這些屬性在IE中是行不通的。

<script type="text/javascript">
    function cancleEvent()
    {
        document.getElementById('a').setAttribute("onclick",'');
        document.getElementById('g').innerHTML="取消事件!";
    }

    function setEvent()
    {
        document.getElementById('a').setAttribute("onclick",'alert("你好!")');//firefox    
        document.getElementById('a').onclick= function () { alert("This is a test!"); }//IE firefox也有作用,但是使用firebug調試看不到修改後的效果,使用上面的方法可以看到。    
        document.getElementById('Div1').innerHTML="設定事件!";
    }
</script>


註:
*p1:使用setAttribute()改變網頁中標籤的onclick屬性    http://blog.csdn.net/Cnami/archive/2008/05/27/2486516.aspx



2、IE中通過 setAttribute添加的屬性 *p2

      IE下setAttribute設定元素的對象、集合和事件屬性都只當成普通屬性,起不到原有的作用,但可以直接進行賦值操作。

<script language="javascript" type="text/javascript">
<!--
function init() {
    var obj = document.getElementById("btn");
    obj.onclick=changeValue;
    obj.style.background="#CC6600";
}
function changeValue() {
    document.getElementById("txt").value = "New value!";
}
-->
</script>


註:
*p2:[求助]通過 setAttribute 添加的屬性在 IE 下不被實現? http://bbs.blueidea.com/viewthread.php?tid=2290521

相關文章

聯繫我們

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