开发者

Strange JQuery Behaviour With Load

I am stuck with this code. Why this doesn't catch body element?

$('#holder').load('Login.html', function(result) {
   alert(result); //shows complete body with <html> and <body>

   var body = $(result).children("body")[0]; //null
   var body = $(result).find("body")[0];     //null 
});

UPDATED:

When I alert 开发者_如何学运维as: alert($(result).children().length); it says 3 (<title>, <meta>, <div>)


Actually I am not sure that you need to deal with the <body> at all. jQuery will automatically just use what's within the <body> tag.

$('#holder').load('Login.html', function() {
   alert($('#holder').html()); // contains just what you want
   // you can now do whatever you wanted to do here
   alert($("#holder div").text()); // text of all <div>s within Login.html
});


According to the documentation http://api.jquery.com/load/

jQuery uses the browser's .innerHTML property to parse the retrieved document and insert it into the current document. During this process, browsers often filter elements from the document such as <html>, <title>, or <head> elements. As a result, the elements retrieved by .load() may not be exactly the same as if the document were retrieved directly by the browser.


Because I believe that what is coming back is text and not dom elements, $(result) would work if it were a dom element but it's just text. Your best bet is to only return an html fragment and not an entire document.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜