javascript - jQuery 有时不改变视频源

标签 javascript html jquery wordpress

在 WordPress 页面中。我有一个标签,每当页面加载时就会自动播放。每次我调整窗口大小时,视频都会重新开始播放。我有一个功能,每次如果网址包含 (/home/en),它都会更改视频 (src) 属性,但由于某种原因,有时不会。

jQuery(window).resize(function() {
        if(window.location.href.includes("/home/en")){
            jQuery('#testID').attr('src','https://test.mp4');
        }
    });

有谁知道可能是什么问题吗!

最佳答案

无法告诉您为什么它有时会失败,除非您的位置不包含您​​想要的内容。但您只需检查一次。

//only need to check this once per window    
var PlayVideo = window.location.href.includes("/home/en");
var PlayTimeout = null;

jQuery(window).resize(function(){
    //adding a timeout to prevent multiple calls
    window.clearTimeout(PlayTimeout);
    PlayTimeout = window.setTimeout(function(){
        if(PlayVideo){
            jQuery('#testID').attr('src','https://test.mp4');
        }
        else{
            //if you reach this point, your condition is not what you want it to be
            console.log(window.location.href)
        }
    }, 1000)
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

关于javascript - jQuery 有时不改变视频源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63248484/

相关文章:

javascript - 为什么我的 javascript if 语句不以数组为目标?

javascript - 为使用节点appendChild添加的列表创建事件监听器

html - Header 之后的部分与 Header 对齐?

html - 我的小部件的 css 在不同网站上的计算方式不同

html - 使用 div 布局的嵌套表格

jQuery 超链接 - href 值?

javascript - window.scroll 不是即时动画

javascript - 如果表单值更改,启用提交按钮

javascript - jQuery:使用切片选择元素

javascript - 带有jsPDF的HTML字符串尾随页面