【问题标题】:if list data contains如果列表数据包含
【发布时间】:2012-01-02 23:13:36
【问题描述】:

我遇到了以下问题

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

我正在尝试遍历包含 data-sockets 值的所有内容以显示和隐藏其他 li 元素。

【问题讨论】:

  • 你的问题是什么? ;-)
  • 我认为这是您代码的总和,因为语法显然非常错误。你的问题的一些更多细节在这里真的会有所帮助。

标签: javascript jquery filter html-lists contains


【解决方案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;
    });

【讨论】:

【解决方案2】:

或者您可以选择如下:

    $('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

【讨论】:

    【解决方案3】:

    $("li").data("sockets") 应该得到它。

    【讨论】:

      【解决方案4】:

      试试这个....

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

      【讨论】:

        猜你喜欢
        • 2021-02-10
        • 2022-01-10
        • 1970-01-01
        • 1970-01-01
        • 2023-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-29
        相关资源
        最近更新 更多