开发者

jQuery find() returns null in only IE

开发者 https://www.devze.com 2023-03-30 12:04 出处:网络
I\'m attempting to load content from another page using jQuery .post(), to parse the returned html to pull out only the pieces that I want.

I'm attempting to load content from another page using jQuery .post(), to parse the returned html to pull out only the pieces that I want.

The problem is that, while this works in Firefox et al, it does not work in IE.

I've tracked down the problem to the find() function. Both IE and FF successfully load the html from the other page (which is stored in that "data" object) using post. However, while the line

debugText += jq(data).find('#timeline_events').html();

produces output in Firefox, it produces null in IE.

I've done extensive research, and as far as I can tell, it might either be because of jquery conflicts, or because the page it's loading is xhtml.

In the first case, it's for that reason that I use var jq=jQuery.noConflict(), and use "jq" instead of "$" 开发者_运维知识库with all jquery calls. In the second case, I've tried declaring the post return data type explicitly to both html and xml, with no change in result.

Any ideas?

<script type="text/javascript" charset="utf-8"> 
var jq=jQuery.noConflict();
var debugText = ""; 
function loadNext() 
{ 
    jq.post("test.html", function(data) 
    {
        debugText += jq(data).find('#myEvents').html();
    }
}
</script>

Update: Here is a sample of some html from the test page that I want to load onto the current page:

<table id="myEvents">
    <tbody>
        <tr>
            <td>Test</td>
        </tr>
    </tbody>
</table>

What I'll eventually want to do is pull out the td elements from that table and insert them into another table on my current page.

-Update- jQuery version I'm using: 1.2.3

Loading the data into a hidden object in the document, and then using jquery to select the necessary id element produces the same result - that is, null in IE, and works otherwise.


IE is very particular with what it "loads" into the DOM. If your content isn't well formed you would get this exact behavior. I had this same problem and it turned out to be that the element in question had a parent that wasn't properly closed.


My guess is that jQuery is parsing the response like it parses XML, which means find, filter, and a number of other methods are broken for some reason in IE.

Try jq('[id=timeline_events]', data)

.html() might also not work; you may need .text().

A better solution might just be to add the response to the page and parse it there.

function(data){
    var $container = $('<div style="display:none">');
    $('body').append($container);
    $container.html( data );
    var html = $container.find('#timeline_events").html();
    $container.remove();
}
0

精彩评论

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

关注公众号