在.get()之后对子类的JQuery过滤

时间:2010-09-20 10:07:09

标签: jquery css-selectors

问题:我想要一个 .get()但只能使用没有 .played 类的元素。 (我正在构建一个随机播放列表,播放的数字会在其div上标记为 .played 类。)

当前代码

var randomElements = $("#playlistVideos").get();

我尝试了什么:过滤器()和很多选择器技巧,但都没有成功。我对你如何解决这样的问题非常感兴趣!

HTML

<div id="playlistVideos">
<div class="vt ddsitem">
    <a href="javascript:loadAndPlayVideo('wQ5iFQv1KaE')"><img class="pt" id="wQ5iFQv1KaE" src="http://i.ytimg.com/vi/wQ5iFQv1KaE/default.jpg"></a>
    <div>
        <img style="width: 18px; height: 18px; position: relative; left: 100px; bottom: 98px;" class="updown" src="images/updown.gif"><a style="cursor: pointer;" href="javascript:deleteVideo('wQ5iFQv1KaE')"><img style="width: 18px; height: 18px; position: relative; left: 150px; bottom: 98px; cursor: pointer;" class="bin" src="images/bin.gif"></a>
    </div>
</div>
<div class="vt ddsitem">
    <a href="javascript:loadAndPlayVideo('Ya3_wQpzDIU')"><img class="pt" id="Ya3_wQpzDIU" src="http://i.ytimg.com/vi/Ya3_wQpzDIU/default.jpg"></a>
    <div>
        <img style="width: 18px; height: 18px; position: relative; left: 100px; bottom: 98px;" class="updown" src="images/updown.gif"><a style="cursor: pointer;" href="javascript:deleteVideo('Ya3_wQpzDIU')"><img style="width: 18px; height: 18px; position: relative; left: 150px; bottom: 98px; cursor: pointer;" class="bin" src="images/bin.gif"></a>
    </div>
</div>
<div class="vt ddsitem played">
    <a href="javascript:loadAndPlayVideo('Dq0hrmT7JiQ')"><img class="pt" id="Dq0hrmT7JiQ" src="http://i.ytimg.com/vi/Dq0hrmT7JiQ/default.jpg"></a>
    <div>
        <img style="width: 18px; height: 18px; position: relative; left: 100px; bottom: 98px;" class="updown" src="images/updown.gif"><a style="cursor: pointer;" href="javascript:deleteVideo('Dq0hrmT7JiQ')"><img style="width: 18px; height: 18px; position: relative; left: 150px; bottom: 98px; cursor: pointer;" class="bin" src="images/bin.gif"></a>
    </div>
</div>
<div class="vt ddsitem">
    <a href="javascript:loadAndPlayVideo('WklNzfvit9E')"><img class="pt" id="WklNzfvit9E" src="http://i.ytimg.com/vi/WklNzfvit9E/default.jpg"></a>
    <div>
        <img style="width: 18px; height: 18px; position: relative; left: 100px; bottom: 98px;" class="updown" src="images/updown.gif"><a style="cursor: pointer;" href="javascript:deleteVideo('WklNzfvit9E')"><img style="width: 18px; height: 18px; position: relative; left: 150px; bottom: 98px; cursor: pointer;" class="bin" src="images/bin.gif"></a>
    </div>
</div>

3 个答案:

答案 0 :(得分:2)

正如其他人所提到的,您可以使用:not()选择器。您也可以使用.not()方法,该方法与 .filter()的工作方式类似,不同之处在于它缩小为与选择器不匹配的元素。

// Using :not()
var randomElements = $('#playlistVideos .vt:not(.played)').get();

// Using .not()
var randomElements = $('#playlistVideos .vt').not('.played').get();

以下是来自jQuery.com的 .not()http://jsfiddle.net/JVNVL/的示例。

答案 1 :(得分:1)

  $("#playlistVideos .vt:not(.played)").get()

应该足够了。

答案 2 :(得分:1)

尝试使用:not选择器:

$('#playlistVideos .vt:not(.played)')