假设我想通过其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的但对我没有帮助。
答案 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" */
答案 1 :(得分:0)
答案 2 :(得分:0)
试试这个:
var data = $('li').data('itemindex', 3).text();
alert(data);