IE10標準模式不支援HTC (Html Components) ,升級重寫Htc原有代碼之二: 事件

來源:互聯網
上載者:User

下面是一個網上流傳很廣的htc例子,例如取名:ie10_htc.htc

<component><PUBLIC:ATTACH EVENT="onmouseover" ONEVENT="hig_lite()" /><PUBLIC:ATTACH EVENT="onmouseout" ONEVENT="low_lite()" />/*其實,換成下面的寫法也可以*//*<ATTACH EVENT="onmouseover" HANDLER="hig_lite"/>*//*<ATTACH EVENT="onmouseout" HANDLER="low_lite"/>*/ <script type="text/javascript">     function hig_lite() {         style.color = 255;     }         function low_lite() {         style.color = 0;     }</script></component>

對應的ie10_htc.htm代碼:

<html><head>    <title id="1">asdf</title>    <meta http-equiv="X-UA-Compatible" content="IE=9" />    <style>    h1{ behavior: url(ie10_htc.htc) }    </style></head><body>  <h1>把滑鼠放在這裡</h1></body></html>

在IE9下運行木有問題,但是到IE9改為IE10,然後用IE10瀏覽就會發現效果沒有了。

升級這段htc的方法倒也不難,如果用純js來寫,可以這樣:

<html><head>    <title id="1">asdf</title>    <meta http-equiv="X-UA-Compatible" content="IE=9" />    <script type="text/javascript">        function hig_lite() {           this.style.color = 255;        }        function low_lite() {            this.style.color = 0;        }        function loaded() {            var hi_Arr = document.getElementsByTagName("h1");            for (var i = 0; i < hi_Arr.length; i++) {                hi_Arr[i].addEventListener("mouseover", hig_lite);                hi_Arr[i].addEventListener("mouseout", low_lite);            }        }          </script></head><body onload="loaded()">  <h1>把滑鼠放在這裡</h1></body>

因為不再依賴htc了,所以在IE9, IE10下瀏覽,都沒問題,效果又回來了,就是代碼有點冗長了。

如果換做用JQuery.js重寫:

<html><head>    <title id="1">asdf</title>    <meta http-equiv="X-UA-Compatible" content="IE=9" />    <script src="jquery-1.7.2.js" type="text/javascript"></script>    <script type="text/javascript">        $(document).ready(function () {            $("h1").mouseover(function () {                $("h1").css("color", "blue");            });            $("h1").mouseout(function () {                $("h1").css("color", "black");            });        });      </script></head><body >  <h1 >把滑鼠放在這裡</h1></body></html>

感覺清爽很多了,同時,顏色不再支援數字,要用英文表示了,這樣也好,一目瞭然,知道是什麼意思。

通過前文(自訂屬性) 和此文(事件) 可以總結出,雖然ms不再支援htc了,但遷移它到js上的代價並不太大,基本上原來的代碼都能用,只不過htc中有些舊的寫法要換成新寫法就好!

聯繫我們

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