find a node in xml using Jquery
Hi I am really new to Jquery and have a problem with my script , I want to access the 2nd 'heading' tag in my xml file using jquery . this is my script so for but what I want to do is assign a vairiable to the 2nd value of heading .
$(document).ready(function()
{
$.ajax({
type: "GET",
url: "task.xml",
dataType: "xml",
success: displayXml
});
开发者_StackOverflow中文版 function displayXml(data){
$(data).find("tasks").each(function() {
var heading = $(this).find("heading").text();
});
}
}); // doc ready
this is my xml doc. What I am looking for is something like
name = $("heading",2).text(); the value being 'New Job'. Can anyone help me with this please ?
<?xml version="1.0" encoding="utf-8"?>
<tasks>
<heading>Home </heading>
<heading>New Job </heading>
<heading>System </heading>
</tasks>
Is this what are you looking for?
$("heading:eq(1)",data).text();
As you can see here in the doc http://api.jquery.com/jQuery/ jQuery()
(same as $()
) receives second parameter "context", in your case this will be data
and jQuery will search in this context instead of your HTML/DOM.
insomniac's reply is, I believe, not quite correct, because :nth-child(2)
returns every other selector, not just the second. The selector you need is :eq(1)
. This gets the second item to match the previous selector:
$(this).find('heading:eq(1)').text();
Note that eq
uses 0-based indexing (i.e. the first item is 0, the second is 1, etc).
See the jQuery API for more information: http://api.jquery.com/eq-selector/
精彩评论