2010-05-05 22 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功能和选择观看关于动态加载事件内容。

相关问题