在 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/