How to get element content by id from HTML object by JavaScript ( JQuery )
i write the following code to access page "JQue开发者_开发问答ryPage.aspx" and get data from it using jQuery
<script type="text/javascript">
$.get
(
"JQueryPage.aspx",
function(data) {
alert("Data Loaded: " + data);
}
);
</script>
"JQueryPage.aspx" is just a page that contain DIV called 'resultsDIV' that contain the data that i want to return
the above code return data variable that contain "JQueryPage.aspx" html and i want to get DIV content from it .. i have 2 questions: 1- how can i extract DIV content from data object 2- is this way is th best to get that data ?Try using something like this:
<script type="text/javascript">
$.get
(
"JQueryPage.aspx", function(html) {
var page = $(html);
var div = $('#div1', page);
}
);
</script>
you can also look into the $.load
function
Jsut wrap the data in a call to jquery and you can use it like you would normally:
$.get
(
"JQueryPage.aspx",
function(data) {
var dataDom = $(data);
$(someSelector, dataDom).each(function(){
alert($(this).html());
});
}
);
- If the html markup of
JQueryPage.aspx
is valid xml, you can use dom parser to get the required div. - It depends - if all you want is to add the retrieved html to the existing DOM using a call to
document.appendChild
, yes. But if you need to parse and read values from the retrieved data, no, this is not. Pass data as a JSON string or xml.
精彩评论