This question already has an answer here:
这个问题已经有了答案:
- jQuery how to find an element based on a data-attribute value? 8 answers
- jQuery如何基于数据属性值查找元素?8回答
<div class="companyname" data-companyname="abc" ></div>
<div class="companyname" data-companyname="xyz" ></div>
<div class="companyname" data-companyname="pqr" ></div>
I need show only data-companyname="abc" div only....so i used following way in jQuery
我需要只显示data-companyname =“abc”div只有....所以我用jQuery遵循的方式
$(".companyname").attr('abc').show();
How to get this?
这个怎么走吗?
3 个解决方案
#1
2
$('div[data-companyname="abc"]').show()
.companyname {
display: none
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="companyname" data-companyname="abc">1</div>
<div class="companyname" data-companyname="xyz">2</div>
<div class="companyname" data-companyname="pqr">3</div>
use the attr selector
使用attr选择器
#2
1
You need to use attribute equals selector
您需要使用属性= selector
Selects elements that have the specified attribute with a value exactly equal to a certain value
选择具有指定属性且值完全等于某个值的元素
$(".companyname[data-companyname=abc]").show();
$(".companyname[data-companyname=abc]").show();
.companyname{display:none}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="companyname" data-companyname="abc" >abc</div>
<div class="companyname" data-companyname="xyz" >xyz</div>
<div class="companyname" data-companyname="pqr" >pqr</div>
#3
0
Try this :Use attribute selector like below
试试这个:使用如下的属性选择器。
$(".companyname[data-companyname='abc']").show();
#1
2
$('div[data-companyname="abc"]').show()
.companyname {
display: none
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="companyname" data-companyname="abc">1</div>
<div class="companyname" data-companyname="xyz">2</div>
<div class="companyname" data-companyname="pqr">3</div>
use the attr selector
使用attr选择器
#2
1
You need to use attribute equals selector
您需要使用属性= selector
Selects elements that have the specified attribute with a value exactly equal to a certain value
选择具有指定属性且值完全等于某个值的元素
$(".companyname[data-companyname=abc]").show();
$(".companyname[data-companyname=abc]").show();
.companyname{display:none}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="companyname" data-companyname="abc" >abc</div>
<div class="companyname" data-companyname="xyz" >xyz</div>
<div class="companyname" data-companyname="pqr" >pqr</div>
#3
0
Try this :Use attribute selector like below
试试这个:使用如下的属性选择器。
$(".companyname[data-companyname='abc']").show();