New to jQuery, I have a script that returns data from an XML file. What I need is the index number of the element returned. I know I can retreve a single element with this
对于jQuery,我有一个从XML文件返回数据的脚本。我需要的是返回元素的索引号。我知道我可以用这个重建一个元素
name = $("sitelist:eq(1)",data).text();
but in my script I need to know each element position for the displayed data. something like position = $(data,eq).value .Can anyone help please .
但是在我的脚本中,我需要知道显示数据的每个元素位置。位置= $(数据,eq)。有谁能帮忙吗?
$(document).ready(function () {
$.ajax({
type: "GET",
url: "sites.xml",
dataType: "xml",
success: displayXml
});
function displayXml(data) {
$(data).find("course").each(function () {
var name = $(this).find("sitelist").text();
var line1 = $(this).find("address1").text();
});
}
}); // doc ready
1 个解决方案
#1
3
I'm not sure which node you need the index for, but if it is the course
that you're iterating over with .each()
you can get the index of each iteration from .each()
我不确定需要为哪个节点建立索引,但是如果是使用。each()进行迭代的课程,则可以从。each()获取每个迭代的索引
$(data).find("course").each(function( idx ) {
alert( idx ); // will alert the index of the current "course"
var name = $(this).find("sitelist").text();
var line1 = $(this).find("address1").text();
});
If there's some other situation, you could try using the .index()
method to get the index of the node from among its siblings. It requires jQuery 1.4 or later.
如果存在其他情况,您可以尝试使用.index()方法从它的兄弟姐妹中获取节点的索引。它需要jQuery 1.4或更高版本。
As in:
如:
var $sitelist = $(this).find("sitelist");
var index = $sitelist.index();
var name = $sitelist.text();
#1
3
I'm not sure which node you need the index for, but if it is the course
that you're iterating over with .each()
you can get the index of each iteration from .each()
我不确定需要为哪个节点建立索引,但是如果是使用。each()进行迭代的课程,则可以从。each()获取每个迭代的索引
$(data).find("course").each(function( idx ) {
alert( idx ); // will alert the index of the current "course"
var name = $(this).find("sitelist").text();
var line1 = $(this).find("address1").text();
});
If there's some other situation, you could try using the .index()
method to get the index of the node from among its siblings. It requires jQuery 1.4 or later.
如果存在其他情况,您可以尝试使用.index()方法从它的兄弟姐妹中获取节点的索引。它需要jQuery 1.4或更高版本。
As in:
如:
var $sitelist = $(this).find("sitelist");
var index = $sitelist.index();
var name = $sitelist.text();