国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > HTML > 正文

IE 下 href 的 BUG

2024-08-26 00:08:59
字體:
來源:轉載
供稿:網友

在瀏覽器 ie6 、ie7、firefox2+、firefpx3+、opera9.6+、safari3.1+中測試以下代碼(demo):

<div id="test">
   
<a href="#"> test </a>
</div>
<div id="result"></div>

<script type="text/javascript">
(function(){
   
var test = document.getelementbyid('test');
    alert
(test.innerhtml);

   
var result =  document.getelementbyid('result');
    result
.innerhtml = test.innerhtml;
    alert
(result.innerhtml)
})();
</script>

結果會發現,在 ie6、ie7 瀏覽器中第二次彈出的 result.innerhtml 中的 a 元素的 href 值成為了絕對路徑。

其實先人們早遇到這些問題(感謝 玉伯 提供的資料):

  • 《getattribute(”href”) is always absolute》
  • 《getattribute href bug》

在上面的文章中已提及了處理方案,就是在 ie 下使用 getattribute( ‘href’ , 2 ) 方法。 microsoft 給此方法擴展了第二個參數,可設置為 0、1、2,如果設置為 2 ,則返回屬性原始值。

腳本修正為:

(function(){
   
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 發現的一個有趣的 bug 問題:在 ie 中當重新設置新的 href 屬性值時,如果鏈接文字含有 “http://” 或 “@” ,則其 innerhtml 將顯示不正確,顯示成設置的 href 屬性。

解決方法(shref 為要設置的 href 新值):

shref = 'http://www.hedgerwow.com';
var ismsie = /*@cc_on!@*/false;
if( ismsie ){
    shref
= ' ' + shref; //add extra space before the new href
};

詳細:《internet explorer might reset anchor’s innerhtml incorrectly when a new “href” is assigned》

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 闽清县| 门头沟区| 乐东| 蓬溪县| 蓬安县| 垦利县| 定边县| 海宁市| 浠水县| 西充县| 余庆县| 射阳县| 三穗县| 白玉县| 那坡县| 山东省| 克山县| 阿拉尔市| 泸水县| 东阳市| 栾城县| 固原市| 治县。| 长治县| 应城市| 两当县| 黑龙江省| 岳阳市| 临澧县| 台前县| 从化市| 长丰县| 台湾省| 白水县| 正宁县| 仁怀市| 永和县| 和田市| 滕州市| 辉县市| 丹棱县|