下面是一個網上流傳很廣的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中有些舊的寫法要換成新寫法就好!