ブラウザIE6、IE7、Firefox2+、FirefPX3+、Opera9.6+、Safari3.1+で次のコード(デモ)をテストします。
<div id = test>
<a href =#> test </a>
</div>
<div id = result> </div>
<script type = text/javascript>
(関数(){
var test = document.getElementById( 'test');
alert(test.innerhtml);
var result = document.getElementById( 'result');
result.innerhtml = test.innerhtml;
アラート(result.innerhtml)
})();
</script>
その結果、結果のA要素のHREF値がIE6で2度目にポップアップし、IE7ブラウザが絶対パスになることがわかります。
実際、私たちの先祖はずっと前にこれらの問題に遭遇しました(提供された情報についてはYu Boに感謝します):
処理ソリューションは、上記の記事で言及されています。これは、IEでgetattribute( 'href'、2)メソッドを使用することです。 Microsoftは2番目のパラメーターをこのメソッドに拡張します。これは0、1、2に設定できます。2に設定すると、プロパティの元の値が返されます。
スクリプトは次のとおりです。
(関数(){
var test = document.getElementById( 'test');
alert(test.innerhtml);
var result = document.getElementById( 'result');
result.innerhtml = test.innerhtml;
if(/*@cc_on!@*/0){// if ie
var links1 = test.getelementsbytagname( 'a');
var links2 = result.getelementsbytagname( 'a');
for(var i = 0、len = links1.length; i <len; ++ i){
links2 [i] .href = links1 [i] .getattribute( 'href'、2);
}
}
alert(result.innerhtml);
})();
この問題を見つけるプロセスでは、Hedger Wangによって見つかった興味深いバグも検索しました。新しいHREF属性値がIEでリセットされると、リンクテキストにhttp://または @が含まれている場合、そのinnerhtmlが誤って表示され、設定されたHREF属性として表示されます。
回避策(Shrefは、設定するHREFの新しい値です):
shref = 'http://www.hedgerwow.com';
var ismsie = /*@cc_on!@* /false;
if(ismsie){
shref = '' + shref;
};
詳細:「インターネットエクスプローラーは、新しいHREFが割り当てられたときにAnchorのInnerHTMLを誤ってリセットする可能性があります」