HTML segment:
<ul id="nav_des">
<li class="cur">test1<a class="shopping2" data-category-id="1">12</a></li>
<li>test2<a class="shopping2" data-category-id="2"></a></li>
<li >test3<a class="shopping2" data-category-id="3"></a></li>
<li style="margin-bottom:25px; background:none"></li>
</ul>
I want to get the text() of "" which "data-category-id" is 1 (i.e. it is 12 ) . I tried to use attribute selector like this but failed. So I just use "for" loop to get the value, but I don't think it's a good way. How to fix it? thanks
var category_id =1
$(".shopping2").("data-category-id='"+category_id+"'").show()
You can use attribute-equals-selector to get elements by their attribute values:
$(".shopping2[data-category-id='"+category_id+"']").show();
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments