简体   繁体   English

用jQuery删除未使用的li类

[英]Remove unused li class with jquery

Here is my HTML 这是我的HTML

<li class="single">
 <ul>
       <li class="franchise0001"></li>
       <li class="franchise0002"></li>
       <li class="franchise0003"></li>
       <li class="franchise0004"></li>
       <li class="franchise0005"></li>
       <li class="franchise0006"></li>
       <li class="franchise0007"></li>
       <li class="franchise0008"></li>
       <li class="franchise0009"></li>
       <li class="franchise0010"></li>
       <li class="franchise0011"></li>
       <li class="franchise0012"></li>
       <li class="franchise0013"></li>
       <li class="franchise0014"></li>
 </ul>
</li>

I run a script that finds out if there are franchises signed up for the league, then it places that franchise logo and name in the correct franchise li class. 我运行一个脚本,查找是否有加盟联盟的特许经营权,然后将特许经营徽标和名称放在正确的特许经营权类别中。 However some franchises are always left empty and never filled. 但是,有些专营权总是空着,从来没有填补过。 Here is an example of the HTML after the script fills in some information 这是脚本填写一些信息后的HTML示例

<li class="single">
 <ul>
   <li><a href="#"><img src=""></a><a>Top Dawg</a></li>
   <li><a href="#"><img src=""></a><a>THEEOhiostate</a></li>
   <li><a href="#"><img src=""></a><a>slambodians</a></li>
   <li><a href="#"><img src=""></a><a>Hollywood Shuffler</a></li>
   <li><a href="#"><img src=""></a><a>Thunderbolts</a></li>
   <li><a href="#"><img src=""></a><a>MeWantee</a></li>
   <li><a href="#"><img src=""></a><a>Cincy Slammers</a></li>
   <li class="franchise0007"></li>
   <li><a href="#"><img src=""></a><a>Mahafaha</a></li>
   <li><a href="#"><img src=""></a><a>SF Drug Lords</a></li>
   <li><a href="#"><img src=""></a><a>Hudy Delight</a></li>
   <li><a href="#"><img src=""></a><a>long shot</a></li>
   <li><a href="#"><img src=""></a><a>Green Guy</a></li>
   <li class="franchise00011"></li>
   <li class="franchise00012"></li>
  </ul>
</li>

I need to remove any empty li not containing an "a" or "img" tag that was generated from the simulation script. 我需要删除任何不包含从模拟脚本生成的“ a”或“ img”标签的空li。

I tried this but it just removes them all , i assume since the HTML has all the li classes empty until script fills them 我试过了,但是它只是删除了所有它们,因为HTML将所有li类都清空了,直到脚本填充了它们

$("li:empty").remove();

Here is a link to the simulation script http://nitrografixx.com/2014/Nike2014/js/teams_simulation.js 这是模拟脚本的链接http://nitrografixx.com/2014/Nike2014/js/teams_simulation.js

First of all they're called "tags"... 首先,它们被称为“标签” ...

Try this: 尝试这个:

$("li:empty").remove();

I see that your script is making an ajax call. 我看到您的脚本正在进行ajax调用。 You need to do the $("li:empty").remove(); 您需要执行$("li:empty").remove(); AFTER the ajax call finishes and the li tags have all been transformed. 在ajax调用完成并且li标签都已转换之后。 If you do the call to remove the empty li calls without waiting for the ajax call to complete, then it's likely that it will remove ALL of the li tags, since it's only after the ajax call returns that the li tags are modified. 如果您执行调用以删除空的li调用而不等待ajax调用完成,那么它很可能会删除所有li标签,因为只有在ajax调用返回后,才对li标签进行修改。

Specifically, add the remover code at the end of your ajax success function as follows: 具体来说,如下所示,在ajax成功函数的末尾添加卸妆代码:

$.ajax({
   . . .
   success : function(data) {
      . . .
       $.each(data, function(index, element) {
          . . .
       });
       $("li:empty").remove(); // do this only AFTER the li tags have been modified
   );
   . . .
);

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

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