jQuery - 通过唯一 ID 对多个对象迭代 API 命令?

发布于 2024-09-30 08:05:12 字数 450 浏览 5 评论 0原文

我在一个页面上有一堆 vimeos,我想让它们全部静音,因为它们将同时播放。

我只在一个 vimeo 上运行。因为它需要每个 vimeo 的唯一 ID。 但我对对视频做不同的事情不感兴趣。我只想给他们一条规则,显然不必每次上传新的 vimeo 时都写出来。

var iframe = document.getElementById("player_1");
iframe.api("api_setVolume", 50);

我尝试让所有 vimeo 的 ID =player_1,但这不起作用。 由于某种原因,JS 中没有 getElementByClass,应该没有吧?对于这样的事情会有用。

任何帮助将不胜感激。谢谢。

这是测试页面

I have a stack of vimeos on a page, I want to make them all mute though as they will be playing simultaneously.

I've got it working on only one vimeo. As it needs the unique ID for each vimeo.
But I'm not interested in doing different things to the videos. I just want to give them one rule, and obviously not have to write it out each time I upload a new vimeo.

var iframe = document.getElementById("player_1");
iframe.api("api_setVolume", 50);

I tried making all the vimeo's ID = player_1, but that doesn't work.
For some reason there's no getElementByClass in JS, there should be no? It'd be useful for things like this.

Any help would be greatly appreciated. Thanks.

Here's the testpage

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感情废物 2024-10-07 08:05:12

由于它被标记为 jQuery,因此我将为您提供一个应该适用于该问题的解决方案,尽管我还没有尝试过使用 iframe 类。

class="vimeo" 添加到 iframe。使用以下代码迭代它们:

$(".vimeo").each(function() {
    this.api("api_setVolume",50);
});

each 将迭代与选择器匹配的所有元素(在本例中为 vimeo)。

编辑

由于 jQuery 似乎不喜欢搞乱 iframe,因此有一种更加编程的方式来完成已经为您工作的操作:

var player_count = 2;
for (var i = 1; i < player_count+1; i++) {
    var iframe = document.getElementById("player_" + i);
    iframe.api("api_setVolume", 50);
    //put other global rules here...
}

现在的技巧是确定 player_count 应该是什么。

Since this is tagged jQuery, I'll give you a solution that should work for that, though I haven't tried messing with iframe classes.

Add class="vimeo" to the iframes. Iterate through them with the following:

$(".vimeo").each(function() {
    this.api("api_setVolume",50);
});

each will iterate through all the elements that are matched by the selector (in this case, vimeo).

EDIT

Since jQuery doesn't seem to like messing with iframes, there's a more programmatic way of doing what's already working for you:

var player_count = 2;
for (var i = 1; i < player_count+1; i++) {
    var iframe = document.getElementById("player_" + i);
    iframe.api("api_setVolume", 50);
    //put other global rules here...
}

The trick now is determining what player_count should be.

缘字诀 2024-10-07 08:05:12

jQuery 将允许您使用以下语法按类选择一组元素: $(".myClass")

http: //api.jquery.com/class-selector/

jQuery will let you select a set of elements by class with the syntax: $(".myClass")

http://api.jquery.com/class-selector/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文