javascript - 如何使用 JavaScript 或 jQuery 将视频静音?

标签 javascript jquery html

我想使用以下广告代码,但在关闭视频后无法将视频静音。当我关闭(另外它会在 20 秒后自动关闭)广告时,它会在后台播放视频并且仍然有声音。单击 SKIP AD 按钮后视频自动关闭时如何静音?我不了解 jQuery 或 JavaScript。你能修改我的代码并发布解决方案吗?

<script>
  window.setTimeout("document.getElementById('closead').style.display='none';", 6000);
</script>
<div class="advertisement" id="closead">
  <a target="_blank" rel="nofollow" href="http://www.sitename.com">
    <video id="dbx" style="object-fit: fill;" autoplay="" width="100%" height="100%">
      <source src="https://www.w3schools.com/tags/mov_bbb.mp4" type="video/mp4">
    </video>
  </a>
  <button onfocus="this.blur();" class="closecss" style="position:absolute;bottom: 2px;right: 0px;z-index: 999;background: #32b02b;color: #fff;padding: 13px;border-radius: 4px;font-weight: bold;cursor: pointer;border: 1px solid #2b9825;box-shadow: inset 0 1px 0 0 rgba(255, 255, 255, 0.15), 0 1px 0 0 #299023, 0 2px 3px 0 rgba(0, 0, 0, 0.25);" onclick="document.getElementById('closead').style.display='none';">SKIP AD</button>
</div>

<style>
  .advertisement {
    position: absolute;
    z-index: 99;
    height: 100%;
  }
</style>

最佳答案

如果能修改上面的脚本,这是个好办法。

<script>
	function removeVIdeo(videoId){
  	var video = document.querySelector(videoId);
    video.muted = true;
  }
  
  window.setTimeout(function(){
  	document.getElementById('closead').style.display='none';
    removeVIdeo('#dbx');
  },6000);
</script>
<div class="advertisement" id="closead">
  <a target="_blank" rel="nofollow" href="http://www.sitename.com">
    <video id="dbx" style="object-fit: fill;" autoplay="" width="100%" height="100%">
      <source src="https://www.w3schools.com/tags/mov_bbb.mp4" type="video/mp4">
    </video>
  </a>
  <button onfocus="this.blur();" class="closecss" style="position:absolute;bottom: 2px;right: 0px;z-index: 999;background: #32b02b;color: #fff;padding: 13px;border-radius: 4px;font-weight: bold;cursor: pointer;border: 1px solid #2b9825;box-shadow: inset 0 1px 0 0 rgba(255, 255, 255, 0.15), 0 1px 0 0 #299023, 0 2px 3px 0 rgba(0, 0, 0, 0.25);" onclick="removeVIdeo('#dbx');">SKIP AD</button>
</div>

<style>
  .advertisement {
    position: absolute;
    z-index: 99;
    height: 100%;
  }
</style>

关于javascript - 如何使用 JavaScript 或 jQuery 将视频静音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50523586/

相关文章:

javascript - 在node.js下使用dojo 1.8 dojo/request失败并出现TypeError : Object has no method 'substring'

javascript - 检查 $(document).ready() 返回的 jQuery 对象内容

javascript - 如何将带有CSS的tr元素动态附加到表中

javascript - 创建一个干净的函数

JavaScript/jQuery : does anybody already created a loading fixed div on top 100%:100% of the page with jquery?

javascript - 未捕获的语法错误 : Unexpected token ILLEGAL in dojo

jquery - 如何将特定行的 HTML 放入 jquery 变量中

javascript - JQuery 数据函数返回错误值

javascript - jquery 查询参数解析 - 查询字符串中的 "="将查询字符串分解

javascript - 检查json中是否存在某个元素