简体   繁体   English

使用Jquery循环遍历列表

[英]Loop through list using Jquery

Total js newb here. 总js newb在这里。

Here is the HTML 这是HTML

<a href="#" class="dur" id="8.5">Size 8.5</a>
 <div class="product1">
 <ul class="sizeAvail" style="display:none;">   
  <li>8</li>    
  <li>8.5</li>  
  <li>9</li>    
  <li>9.5</li>  
  <li>10</li>
 </ul>
 </div>
 <div class="product2">
 <ul class="sizeAvail" style="display:none;">   
  <li>8</li>    
  <li>8.5</li>  
  <li>9</li>    
  <li>9.5</li>  
 </ul>
 </div>

Here's the 'logic' of what I need... 这是我需要的“逻辑”......

  • When the user clicks the Link 当用户单击链接时
  • Capture the id of that element 捕获该元素的id
  • Set that as a variable 将其设置为变量
  • Loop through li for all ul that have class 'sizeAvail' 循环遍历所有具有类'sizeAvail'的ul
  • If li element matches variable 如果li元素匹配变量
  • stop looping and move onto next ul 停止循环并转到下一个ul
  • If ul does not have li that matches variable 如果ul没有匹配变量的li
  • set class of container div to 'hide' 将容器div的类设置为'hide'
  • This is where I'm at so far...any help would be greatly appreciated. 这是我到目前为止......任何帮助将不胜感激。

    <script type = "text/javascript" > $(document).ready(
     $(".dur").click(function () {
     var clickedSize = $(this).attr("id");
     $(".sizeAvail").each(function (li,+) {
        alert($(this).text());
     });
    }); 
    </script>
    

    Here is a working fiddle: 这是一个工作小提琴:

    http://jsfiddle.net/Hive7/uZTYf/ http://jsfiddle.net/Hive7/uZTYf/

    Here is the jquery I used: 这是我使用的jquery:

    $(".dur").click(function () {
        var clickedSize = this.id;
        $(".sizeAvail li").each(function () {
            if($(this).text() == clickedSize) {
                $(this).parent().show();
            } else {
                $(this).hide();
            }
        });
    });
    

    What you are currently doing is not right as you aren't looping through the children of .sizeAvail because you didn't directly state though what you did state wasn't in quotes like most aspects of jquery need to be. 你正在做的事情是不正确的,因为你没有循环遍历.sizeAvail的孩子,因为你没有直接陈述你所做的状态不是像jquery的大部分方面都需要的引号。

    If this still does not work make sure you have a jquery library 如果这仍然不起作用,请确保您有一个jquery库

    Or you can use the pure js option: 或者您可以使用纯js选项:

    var $items = document.getElementsByClassName('sizeAvail');
    var $dur = document.getElementsByClassName('dur');
    for (i = 0; i < $dur.length; i++) {
        $dur[i].addEventListener('click', durClick);
    }
    
    function durClick() {
        var clickedSize = this.id;
        for (i = 0; i < $items.length; i++) {
            var $liElems = $items[i].getElementsByTagName('li');
            for (i = 0; i < $liElems.length; i++) {
                if ($liElems[i].innerHTML == clickedSize) {
                    $liElems[i].parentNode.style.display = 'block';
                    $liElems[i].style.display = 'block';
                } else {
                    $liElems[i].style.display = 'none';
                }
            }
        }
    }
    

    http://jsfiddle.net/Hive7/uZTYf/2/ http://jsfiddle.net/Hive7/uZTYf/2/

    everything you are trying to do is pretty simple syntax-wise. 你想要做的每件事都是非常简单的语法。 you can find documentation on methods to use in a number of places. 您可以找到有关在许多地方使用的方法的文档。 you could simply use javascript for this but i am assuming you want to use jQuery 你可以简单地使用javascript,但我假设你想使用jQuery

    on a high level you'll want to use the jQuery selector to get all UL objects and then for each UL loop over all LI children, eg: 在高级别上,您将希望使用jQuery选择器获取所有UL对象,然后使用所有LI子级的每个UL循环,例如:

    $('ul').each(function() {
        $(this).find('li').each(function(){
    
        });
    });
    

    to get the data you are looking for, you can use jQuery methods like addClass(), attr(), etc. 要获取您正在寻找的数据,您可以使用jQuery方法,如addClass(),attr()等。

    You can use a combination of not , has and contains selectors to get the matched elements and set a class on them using addClass . 您可以使用nothascontains选择器的组合来获取匹配的元素,并使用addClass在它们上设置类。

    Ref: 参考:

    http://api.jquery.com/not-selector/ http://api.jquery.com/not-selector/

    http://api.jquery.com/has-selector/ http://api.jquery.com/has-selector/

    http://api.jquery.com/contains-selector/ http://api.jquery.com/contains-selector/

    http://api.jquery.com/addClass/ http://api.jquery.com/addClass/

    Code: 码:

    $(".dur").click(function () {
        $(".sizeAvail:not(:has(li:contains('"+$(this).prop("id")+"')))").addClass('hide')
    });
    

    Demo: http://jsfiddle.net/IrvinDominin/t8eMD/ 演示: http//jsfiddle.net/IrvinDominin/t8eMD/

    You may use this. 你可以用它。 Set a flag when checking all li's of a div. 检查div的所有li时设置标志。 If none li has same value as the id, at the end hide the div. 如果没有li与id具有相同的值,则最后隐藏div。

      $(document).ready(function(){
        $(".dur").click(function () {
          var clickedSize = this.id;
          $(".sizeAvail").each(function(){
              var hide = 1;
              $(this).children('li').each(function(){
                if(clickedSize == $(this).text()) hide=0;
              });
              if(hide){
                  $(this).closest('div').hide(); //Or $(this).parent().hide();
              }
          });
        }); 
      });
    

    JSFIDDLE 的jsfiddle

    You may try this too 你也可以尝试一下

    $('a.dur').on('click', function(e){
        e.preventDefault();
        var id = this.id;
        $('ul.sizeAvail li').each(function(){
            if($(this).text() == id) $(this).closest('ul').addClass('hide');
        });
    });
    

    EXAMPLE. 例。

    Demo: http://jsfiddle.net/QyKsb/ 演示: http //jsfiddle.net/QyKsb/

    This is one way to do it, as you were doing. 正如您所做的那样,这是一种方法。 However, i don't, personally like cluttering html with data as that. 但是,我不喜欢,个人喜欢用数据混乱html。 But it maybe good choice in some situations but i dont like it. 但它在某些情况下可能是不错的选择,但我不喜欢它。

    also you cant give id a value that starts with numbers. 你也不能给id一个以数字开头的值。

    var products= $("div[class^='product']"),
        dur =$('.dur');
    
    dur.click(change);
    function change(){
       var size= $(this).data('size');
        products.each(function(){
            var d = $(this);
            d.find('.sizeAvail>li').each(function(){
                d.hide(); 
                if($(this).text()==size) { d.show(); return false;}
            });
        });
    }
    

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM