jQuery .find() in server response

三世轮回 提交于 2019-12-11 11:09:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!