问题
I am trying to get the text inside a specific div in a server response. I used Firebug to see what the response was, and I can see my element in the returned code, but for some reason I can get jQuery to capture it. Here is what I am using:
var response = $('<div />').html(serverData);
$('#uploadedFiles').html($(response).find("#created").text());
alert($(response).find("#created").text());
Trying that just returns nothing, not text or anything. Am I doing this correctly?
Note: The server response is not from a jQuery ajax function, rather from the jQuery SWFUpload plugin, would this matter though?
回答1:
When are you running the code? If you run it before the uploadedFile element is created, the code will not find it.
I tested this, and it works just fine, it alerts "asdf" and then replaces "test" with "asdf" in the div element:
<script type="text/javascript">
$(function(){
var response = $('<div />').html('<div id="created">asdf</div>');
alert(response.find("#created").text());
$('#uploadedFiles').html(response.find("#created").text());
});
</script>
<div id="uploadedFiles">test</div>
Note that response is alread a jQuery object, so $(response) is redundant.
来源:https://stackoverflow.com/questions/1564679/jquery-find-in-server-response