我遇到了以下问题
<a href="" class="so" title="Schuko"></a>
<a href="" class="so" title="French CEE17"></a>
$('a.so').live("click", function () {
var filter = $(this).attr("title");
if (filter) {
$('li:contains('+$(this).attr("title")+')').each(function(){
alert('yes');
});
}
return false;
});
<ul class="product_content">
<li class="prod" data-sockets="UK 15A CEE22 Schuko French Swiss Danish ">text goes here</li>
<li class="prod" data-sockets="UK 15A Schuko French CEE17 (16A) Socapex Harting Dutch Harting ">text goes here</li>
</ul>
我试图循环遍历包含数据套接字值的所有内容以显示和隐藏其他 li 元素。
最佳答案
$('a.so').live("click", function () {
var filter = $(this).attr("title");
if (filter) {
// loop only trough li-s that have data-sockets attr
$('li[data-sockets]').each(function(){
if ($(this).find(':contains('+filter+')'))
{
alert('yes');
}
});
}
return false;
});
第二个版本:
$('a.so').live("click", function () {
var filter = $(this).attr("title");
if (filter) {
// loop only trough li-s that have data-sockets attr
$('li[data-sockets]:contains('+filter+')').each(function(){
$(this).fadeOut();
});
}
return false;
});
关于javascript - 如果列表数据包含,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8212080/