Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/437.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果列表数据包含_Javascript_Jquery_Filter_Html Lists_Contains - Fatal编程技术网

Javascript 如果列表数据包含

Javascript 如果列表数据包含,javascript,jquery,filter,html-lists,contains,Javascript,Jquery,Filter,Html Lists,Contains,我有以下问题 <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) {

我有以下问题

<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>

$('a.so').live(“单击”,函数(){
var filter=$(this.attr(“title”);
如果(过滤器){
$('li:contains('+$(this).attr(“title”)+'))。each(function(){
警惕(“是”);
});      
}       
返回false;
});
  • 文本如下
  • 文本如下
我正在尝试遍历包含数据套接字值的所有内容,以显示和隐藏其他li元素。

$(“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;
    });

或者您可以按以下方式选择:

    $('a.so').live("click", function () {   
        var filter = $(this).attr("title");

        $('li[data-sockets*="'+filter+'"]').each(function() {
            alert( 'yes' );
        });
        return false;
    });
这使用jquery

jshiddle

试试这个

<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>

$(文档).ready(函数(){
$('a.so').live(“单击”,函数(){
var filter=$(this.attr(“title”);
如果(过滤器){
//隐藏所有列表元素
$('li')。每个(函数(){
$(this.hide();
});                    
//显示仅包含筛选器字符串的元素
$('li:contains('+filter+'))。每个(函数(){
$(this.show();
});      
}       
返回false;
});
});
  • UK 15A CEE22 Schuko French Swiss Danis
  • “UK 15A Schuko French CEE17(16A)Socapex Harting Dutch Harting

那么你的问题是什么?;-)我假设这是你的代码的总结,因为语法显然是非常错误的。关于你的问题的更多细节在这里会很有帮助。您好,首先感谢您的回答,但它似乎不只是包含值的那一个。