开发者

Jquery - how do you search for an element using an attribute value you set using .data

I开发者_如何学Pythonm setting index numbers on a html lists item $("li").data("index", someValue).

I don't know how to find a specific li based on this index value. Ive searched on here and Ive seen a few posts re: html5 data attributes but Im using JQuerys .data function to append data to the dom and not to the html itself.

Can anyone help?


Using a filter.

 $('selector').filter( function() { return $.data(this,'foo') == 'desired'; } )
              ...


I usually set an id to the li that help retrieve it easily (if I understood your problem). Something like :

<ul>
   <li id="my-list-id-1">...</li>
   <li id="my-list-id-2">...</li>
   <li id="my-list-id-3">...</li>
   <li id="my-list-id-4">...</li>
</ul>

And then, to find the specific li, it's just as easy as :

$('li#my-list-id-' + index)

Hope this helps.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜