首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >视频元素索引的提取及其在函数中的应用

视频元素索引的提取及其在函数中的应用
EN

Stack Overflow用户
提问于 2021-09-22 00:05:30
回答 1查看 47关注 0票数 0

我有两个视频元素在一个页面上,将或不会显示基于视口大小。其中每一个都有自己的play按钮,这会触发一些函数,这些函数使用索引来针对特定的玩家。我想知道是否有更好的方法,如果迭代播放按钮,然后应用正确的索引?

代码语言:javascript
复制
$(window).resize(function () {
    if ($(this).width() > 768) {
        $(".toggle-1").on("click", function () {
            playPause(0);
            anotherFun(0)
        });
    } else {
        $(".toggle-2").on("click", function () {
            playPause(1);
            evenMoreFun(1)
        });
    }
});
代码语言:javascript
复制
function playPause(index) {
    // player[index].togglePlay();
}
function anotherFun(index) {
    // code[index]
    // more stuff
}
function evenMoreFun(index) {
    // code[index]
    // more stuff
}   
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-22 17:08:18

使用IDs而不是索引:

代码语言:javascript
复制
$(window).resize(function () {
    if ($(this).width() > 768) {
        $(".toggle-1").on("click", function () {
            playPause("vid1");
            anotherFun("vid1")
        });
    } else {
        $(".toggle-2").on("click", function () {
            playPause("vid2");
            evenMoreFun("vid2")
        });
    }
});



function playPause(id) {
    const element = document.getElementByID(id)
    element.togglePay()
}
function anotherFun(id) {
    const element = document.getElementByID(id)
    // do stuff on element
}
function evenMoreFun(id) {
    const element = document.getElementByID(id)
    // do stuff on element
}   

只需确保将ID作为字符串传递到playPauseanotherFun中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69276793

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档