2010-05-05 23 views
2

場景:我使用jQuery來延遲加載一些html,並將所有錨點的相對href屬性更改爲絕對鏈接。在IE中通過.load添加html中的jQuery選擇()

加載函數在所有瀏覽器中添加html。

url重寫函數適用於所有瀏覽器中的原始DOM。

在IE7,IE8,我不能運行在DOM新延遲加載的HTML相同的功能。

//lazy load a part of a file 
$(document).ready(function() { 
    $('#tab1-cont') 
     .load('/web_Content.htm #tab1-cont'); 
    return false; 
}); 
//convert relative links to absolute links 
$("#tab1-cont a[href^=/]").each(function() { 
    var hrefValue = $(this).attr("href"); 
    $(this) 
     .attr("href", "http://www.web.org" + hrefValue) 
     .css('border', 'solid 1px green'); 
    return false; 
}); 

我想我的問題是:什麼竅門讓IE瀏覽器在延遲加載jQuery的DOM上進行選擇?

這是我的第一篇文章。溫柔:-)

感謝,

喬爾

回答

0

如果你曾經想使用jquery .load()延遲加載一些HTML,你需要知道IE和FF不會以相同的方式處理相對的HREF。 IE向相對URL添加一個幻影域(對於加載的惰性html)。因此,如果你加載一個充滿HREF的內容的DIV,你不能指望用$(a [href^=//])來找到所有相關的HREF。 IE將不會有任何。

所以我寫了這一點:

(。?多多包涵,我是一個CSS的傢伙誰做了一些jQuery的我知道這可能會更加文筆優美有人能幫助我與)

$('.footer-wrap').ready(function() { 
    $('.tab1-cont').load('/web_Content.htm .tab1-cont'); 
    return false; 
}); 
$(".footer-wrap").hover(function() { 
    //Step 1 
    //IE seems to add a ghost domain to lazy loaded html. In my case, "site.heritage" is the domain, so IE added to lazy loaded href URL. 
    //So I sniff for "site" in the URL 
    $(".footer-wrap a[href*=site]") 
    //on all results 
    .each(function() { 
     //clean up the ghost domain by getting the href and replacing "http://site.web.org" with nothing 
     var hrefValue = $(this).attr("href").replace('http://site1.web.org', '').replace('http://site2.web.org', '').replace('http://site.web.org', ''); 
     //add the proper domain to the clean relative href URL 
     $(this).attr("href", "http://www.web.org" + hrefValue); 
    }); 
    //Step 2 
    //FF just needs this to make a relative url into an absolute URL because it treats lazy loaded html like regular html. 
    $(".footer-wrap a[href^=/]") 
    .each(function() { 
     var hrefValue = $(this).attr("href"); 
     $(this).attr("href", "http://www.web.org" + hrefValue); 
    }); 
}); 

也許IE中的惰性加載HREF不會爲所有人一直採用這種方式......我只是發現了一些小衆情況。我不知道。希望這篇HREF文章能夠節省一些時間。

1

HVE您嘗試使用您的轉換邏輯的回調爲load

$(document).ready(function() { 
    $('#tab1-cont') 
     .load('/HeritageFoundation_Content.htm #tab1-cont', function(html){ 
      $('a[href^=/]', html).each(function(){ 
      var hrefValue = $(this).attr("href"); 
      $(this) 
       .attr("href", "http://www.heritage.org" + hrefValue) 
       .css('border', 'solid 1px green'); 
      return false; 
      }); 
    }); 
    return false; 
}); 
+0

謝謝,但我無法使用此代碼成功運行這兩個函數。 GET請求加載了html。但在FF中,瀏覽器忽略了在插入到DOM之前我們試圖在html上運行的.attr和.css方法。 FF對待它的方式與IE對待我的原始代碼的方式相同。謝謝你的建議。任何其他想法? – 2010-05-06 13:28:16

0

您可以在文本運行jQuery的你已經從阿賈克斯加載後,您將其插入到DOM之前,或者您可以使用livedelegate功能和選擇觀看關於動態加載事件內容。

相關問題