通过.load()添加在IE上的IE中进行jQuery选择

时间:2010-05-05 19:28:07

标签: jquery

场景:我使用jQuery延迟加载一些html并将所有锚点的相对href属性更改为绝对链接。

加载功能在所有浏览器中添加html。

url rewrite函数适用于所有浏览器中的原始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上进行选择?

这是我的第一篇文章。要温柔: - )

谢谢,

乔尔

3 个答案:

答案 0 :(得分:1)

您是否尝试将转换逻辑用作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;
});

答案 1 :(得分:0)

您可以在从ajax加载文本之后,在将文本插入DOM之前对文本运行jQuery,或者可以使用livedelegate函数和选择器来监视动态加载内容的事件。

答案 2 :(得分:0)

如果你曾经想使用jquery .load()延迟加载一些HTML,你需要知道IE和FF不会以相同的方式处理相对的HREF。 IE将鬼域添加到相对URL(延迟加载的html)。

因此,如果您加载了一个充满HREF的内容的DIV,您就不能指望使用$(a [href ^ = /])来查找所有相对的HREF。 IE不会有任何。

所以我写了这个:

(跟我说吧,我是一个做一些jQuery的CSS人。我知道这可以写得更优雅。有人可以帮我解决这个问题吗?)

$('.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帖子可以节省一些时间。