开发者

Using jQuery, how do I get the index of an element found in XML?

I have an XML file setup like so:

<entry name="bob"></entry>
<entry name="ryan"></entry>
<entry name="joe"></entry>
...
<entry name="etc"></entry>

Next, I have a line of code that picks out a name from the XML like so:

var $user= $('entry[images="' + 开发者_开发问答userName + '"]', xml);

But how do I find out what the index of $user is in the overall XML? Example: if userName was 'joe', I should get the number '2' back. Any suggestions?


You can use the .index() method: http://api.jquery.com/index/


I believe that this is what you are looking for: http://api.jquery.com/index/


jQuery's index() method is your friend. Look at this jQuery:

<script type="text/javascript">
window.onload = function() {
    $("div p").each(function() {
        $(this).append( $(this).attr("name")+$(this).index() );
    });
};
</script>

HTML:

<div>
<p name="tom"></p>
<p name="dick"></p>
<p name="harry"></p>
</div>

will produce:

tom0

dick1

harry2
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜