我有一系列使用数据数据描述符“HTML5 -type2=x”标记的图像,其中x是许多不同的元素。
例如:
<img data-type2="pants" class="element" src="#>我正在尝试将该数据字段传递给一个jquery函数,该函数在另一个div div中查找类,该div div具有用类标记的子(<div class="outfit-list"),例如:
<div class="pants-001">
<div class="pants-002">
<div class="shoes-001">等。
这就是我被难住的地方:我如何编写一个jquery函数,从我单击的项中访问数据类型(例如,type2 -type2= "pants“),在.outfit-list下找到所有其他div,其中的类的类名为"pants-002",并隐藏它们?下面的函数不起作用--我怀疑这是因为它查找的是全名而不是部分名。
如何让它执行部分搜索,以定位包含来自data-type2的术语的类?
<script type="text/javascript">
$(document).ready(function(){
$('.thumbslist .element').click(function(){
$('.outfit-list').find('.'+$(this).data('type2')).hide();
});
});
</script> 发布于 2011-07-13 13:09:27
您可以使用[attribute*="value"]的attribute contains selector。
$('.outfit-list').find('[class*="' + $(this).data('type2') + '"]').hide();发布于 2011-07-13 13:08:52
您可以使用starts with选择器。就像这样
$(".thumbslist .element").click(function() {
var type2 = $(this).data("type2");
$(".outfit-list").find("div[class^=" + type2 + "]").hide();
});发布于 2011-07-13 13:08:30
这个插件增加了对数据选择器的支持:http://plugins.jquery.com/project/dataSelector
https://stackoverflow.com/questions/6674296
复制相似问题