为什么这个jQuery表达式只返回第一个元素?

时间:2017-08-08 17:19:18

标签: jquery

很奇怪,这个视频组合:https://www.youtube.com/watch?v=KbNXnxwMOqU

在描述中包含一个轨道列表,其中包含要跳转到的轨道的时间帧:

class VideoMode {
public:
    int64 width;
    int64 height;
    std::vector<int64> frequencies;
}

class Display {
protected:
    std::vector<VideoMode> modes_;
    std::string display_name_;
    std::string adapter_name_;
    bool primary_;

public:
    Display(char* osDevName, char* osAdapterDevName);

    // typical getters
}

在Chrome控制台中,我尝试获取这些链接的列表(带有attr onclick的所有锚标签,以“yt.www.watch.player.seekTo”开头):

<a href="#" onclick="yt.www.watch.player.seekTo(00*60+00);return false;">00:00</a>
<a href="#" onclick="yt.www.watch.player.seekTo(04*60+11);return false;">04:11</a>
<a href="#" onclick="yt.www.watch.player.seekTo(08*60+04);return false;">08:04</a>

它只返回第一个锚标记。为什么不呢?

或者,也许它根本不是jQuery,那么选择器的工作原理是什么?

enter image description here

更新:我的错误,它甚至不是jQuery,谷歌使用的CommandLineAPI,它有不同的语法。问题已经结束。欢呼声。

2 个答案:

答案 0 :(得分:0)

看起来$(selector)的结果只是一个jQuery对象。因此,它也可能返回attr seletor的jquery对象。

但是你可以在这种情况下使用map来使它工作,attrs将是一个数组:

var attrs = $.map($("a[onclick^='yt.www.watch.player.seekTo']"), function(element) {
  return $(element).html(); // or whatever attribute of the element
});

阅读有关jquery选择器的更多信息:https://www.w3.org/TR/css3-selectors/#attribute-selectors

答案 1 :(得分:0)

似乎Youtube以某种方式包装jquery。这对我有用:

$$("a[onclick^='yt.www.watch.player.seekTo']")

其中$$似乎是原始的JQuery实例。希望它有所帮助

相关问题