循环遍历所有元素(动态驱动的内容),如果它们仍然具有特殊属性 [英] loop through all elements (dynamically driven content) if they all still have special attrbitue

查看:16
本文介绍了循环遍历所有元素(动态驱动的内容),如果它们仍然具有特殊属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要的东西(使用 Jquery 的概率)是循环遍历我的 ul(动态驱动的内容)中的所有元素,如果它们仍然具有这个特殊的属性 pl_id(数值可能会有所不同)

如果此 ul 中的所有元素仍包含该属性,则该按钮仍需要就位.如果添加了一个元素,按钮必须消失......我很困惑如何解决这个问题.

    <li pl_id="14" class="item" id="5" ytid="SnA52s7qceM" Artist="Michael Bublé" albname="Christmas" track="圣诞老人来城了" cdnr="1" trnr="2" length2="02:51"></li><li class="item" id="1" ytid="8OsgqpnuizY" Artist="Michael Bublé" albname="Christmas" track="White Christmas" cdnr="1" trnr="4" length2="03:37"></li>

所以在这种情况下,第二个 li 没有 pl_id attr.所以我的喜欢按钮(或任何功能)需要消失.只要这个 ul 中的所有 li 都有这个属性,按钮就需要在那里!谢谢.

解决方案

理论上以下代码可能对您有所帮助:

$("#buttonID").toggle($("ul > li").filter(function() {返回 !this.hasAttribute("pl_id");}).长度=== 0);

演示: http://jsfiddle.net/HBbk3/>

The thing I need (prob with Jquery) is to loop through all elements in my ul (dynamically driven content) if they all still have this special attrbitue pl_id (the value may vary)

If all elements within this ul still contain the attribute then the button still needs to be in place. If one element is added the button has to disappear... I'm confused how to approach this.

<ul id="tracks"> 
<li pl_id="14" class="item" id="5" ytid="SnA52s7qceM" artist="Michael Bublé" albname="Christmas" track="Santa Claus Is Coming to Town" cdnr="1" trnr="2" length2="02:51"></li> 
<li class="item" id="1" ytid="8OsgqpnuizY" artist="Michael Bublé" albname="Christmas" track="White Christmas" cdnr="1" trnr="4" length2="03:37"></li>
</ul> 

So in this case the second li does not have the pl_id attr. So my like button (or whatever function) needs to disappear. As soon as ALL the li's in this ul have this attr the button needs to be there! Thanks.

解决方案

Theoretically the following code might help you:

$("#buttonID").toggle($("ul > li").filter(function() {
    return !this.hasAttribute("pl_id");
}).length === 0);

DEMO: http://jsfiddle.net/HBbk3/

这篇关于循环遍历所有元素(动态驱动的内容),如果它们仍然具有特殊属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆