IEでHTMLElement.prototypeの代替手段
FireFoxなどにあるHTMLElementのprototypeに共有の便利メソッドを突っ込みたいという欲求があるんですがIEでは実装されていないのでうまくいきません。googleっても出てこねぇよ!ということで仕様読む。MSDN見ていたらどうやらIEではDOMイベントなどの定義にはprototypeではなくHTC(HTML Component)という機構で実装されているからだそうで…*1
DIVエレメントに共通のonclickイベントを仕込む
以下のスクリプトは全てのDIV要素にonclickイベントを仕組むスクリプトです。alert("hoge");されます。
■hoge.htc <public:component> <public:attach event="onclick" onevent="hoge()" /> <script language="JScript"> function hoge(){ alert("hoge"); }; </script> </public:component> ■hoge.html <html> <head> <style type="text/css"> div { width: 100px; height: 100px; behavior : url("hoge.htc"); // ← 重要!! } </style> </head> <body> <div style="background-color:red">A</div> <div style="background-color:navy">B</div> <div style="background-color:yellow">C</div> <div style="background-color:black">D</div> </body> </html>
*1:現在調査中なので真偽は保障しません。