我遇到了以下问题
<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元素。
答案 0 :(得分:1)
$('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;
});
答案 1 :(得分:1)
或者你可以选择如下:
$('a.so').live("click", function () {
var filter = $(this).attr("title");
$('li[data-sockets*="'+filter+'"]').each(function() {
alert( 'yes' );
});
return false;
});
这使用jquery attribute contains selector
jsfiddle here
答案 2 :(得分:0)
$("li").data("sockets")
应该得到它。
答案 3 :(得分:0)
试试这个......
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('a.so').live("click", function () {
var filter = $(this).attr("title");
if (filter) {
// hide all the list elements
$('li').each(function(){
$(this).hide();
});
// show the elements that contains only filter string
$('li:contains('+filter+')').each(function(){
$(this).show();
});
}
return false;
});
});
</script>
</head>
<body>
<a href="" class="so" title="Schuko">Schuko</a>
<a href="" class="so" title="French CEE17">French CEE17</a>
<ul class="product_content">
<li class="prod" data-sockets="UK 15A CEE22 Schuko French Swiss Danish ">UK 15A CEE22 Schuko French Swiss Danis</li>
<li class="prod" data-sockets="UK 15A Schuko French CEE17 (16A) Socapex Harting Dutch Harting ">"UK 15A Schuko French CEE17 (16A) Socapex Harting Dutch Harting </li>
</ul>
</body>
</html>