开发者

get index value of an element from XML using jQuery

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

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 .

$(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


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()

 $(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.

As in:

var $sitelist = $(this).find("sitelist");
var index = $sitelist.index();
var name = $sitelist.text();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜