如何通过data-itemindex获取元素?

时间:2014-04-14 12:40:46

标签: javascript jquery html dom

假设我想通过其data-itemindex得到innerHTML以下<li>。我甚至都不知道这是不可能的。

<li id="li:90" class="liGrid" data-itemindex="3" data-itemid="li:90" >
winoria</li>

我试过

alert($("li").find("[data-itemindex=3]").html());
alert($("li[data-itemindex='3']").text());
来自How to select elements with jQuery that have a certain value in a data attribute array

但对我没有帮助。

3 个答案:

答案 0 :(得分:1)

使用CSS标签选择器在DOM中找到匹配的元素:

$("[data-itemindex=3]")

您甚至可以使用类似的语法执行更高级的选择器:

[title~=flower] /* Selects all elements with a title attribute containing the word "flower" */
[lang|=en] /* Selects all elements with a lang attribute value starting with "en" */
a[src$=".pdf"] /* Selects every <a> element whose src attribute value ends with ".pdf" */
a[src^="https"] /* Selects every <a> element whose src attribute value begins with "https" */

Full documentation

答案 1 :(得分:0)

您可以使用:

 $('li[data-itemindex="3"]').text();

 $('li[data-itemindex="3"]').html()

<强> Working Demo

答案 2 :(得分:0)

试试这个:

var data = $('li').data('itemindex', 3).text();
alert(data);