Remove li tags using javascript array

前端 未结 4 1296
情深已故
情深已故 2020-12-11 16:35

I\'m searching a solution to remove

  • tags filtered using javascript array.

    Array:

    var mygroups=[\"1\",\"8\",\"3\",\"4\",\"5\"]         
    
    
            
  • 相关标签:
    4条回答
    • 2020-12-11 17:14

      This works

      $("li").each(function() {
                if($(this).find("a").size() == 0) {
                    $(this).remove();
                };
          });
      
      0 讨论(0)
    • 2020-12-11 17:16

      This seems to do what you want. It's not the tidiest code in the world though:

      $('li').filter(function() {
          var a = $(this).find('a');
          if (!a.length)
              return true;
      
          var text = /(\d+)-/.exec(a.attr('href'))[1];  
          if ($.inArray(text , mygroups) >= 0)
              return false;
      
          return true;
      }).remove();
      

      It's getting all the li's, then filtering out any that have a matching value to the array and removing the ones that are left.

      http://jsfiddle.net/8AmNR/5/

      0 讨论(0)
    • 2020-12-11 17:17
      $('li').filter(function() {
          if($(this).is('*:has(a[href|=/group/viewgroup/])') {
              var href = $(this).find('a').attr('href'),
                  trail = href.split('/')[3],
                  number = + /(\d+)-/.exec(trail)[1];
              return $.inArray(myarray, +number) == -1;
          }
          return true;
      }).remove();
      
      0 讨论(0)
    • 2020-12-11 17:18

      How about using a filter function.

      In this scenario you select all the li and filter out the desired one.

      $('li').filter(function(index){
          if(/*condition applied*/){
              ....
          }
      });
      
      0 讨论(0)
    提交回复
    热议问题