我有一系列用 HTML5 数据描述符“data-type2=[x]”标记的图像,其中 x 是许多不同的元素。
例如
<img data-type2="pants" class="element" src="#>
我正在尝试将该数据字段传递给一个 jquery 函数,该函数在另一个 div (<div class="outfit-list")
中查找类具有标记有类的子 div,例如:
<div class="pants-001">
<div class="pants-002">
<div class="shoes-001">
等等
这就是我被难住的地方:我如何编写一个 jquery 函数来访问我单击的项目中的数据类型 2(例如 data-type2="pants"),找到 .outfit-list 下的所有其他 div 以及具有的类,例如,他们的类(class)名称“pants-002”中的“pants”,并隐藏它们?我在下面的功能不起作用 - 我怀疑那是因为它正在寻找全名而不是部分名称。
如何让它执行部分搜索以找到包含来自 data-type2 的术语的类?
<script type="text/javascript">
$(document).ready(function(){
$('.thumbslist .element').click(function(){
$('.outfit-list').find('.'+$(this).data('type2')).hide();
});
});
</script>
最佳答案
您可以使用 attribute contains selector , [attribute*="value"]
.
$('.outfit-list').find('[class*="' + $(this).data('type2') + '"]').hide();
关于javascript - 如何对动态 HTML5 数据描述符执行部分查找?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6674296/