使用 jquery 将声音从静音更改为取消静音

Change sound from mute to un mute using jquery

本文关键字:取消 jquery 声音 使用      更新时间:2023-09-26

我正在尝试切换YouTube视频的数据属性,这是我目前拥有的HTML

<a id="bgndVideo" class="player" data-property="{videoURL:'https://youtu.be/KW2JUfgQct0',containment:'.video-section', quality:'high', autoPlay:true, mute:true, opacity:1}">bg</a>
<a class="muteButton" href="#">Mute</a>

我正在尝试将数据属性从"静音:真"更改为"静音:假"

我找到了一个教程,它在那里工作,但它不是数据,而是说 prop

 $(document).ready(function(){
     $(".muteButton").click( function (){
        $(this).data('mute', !$(this).data('mute'));
    });
    });

这在我发现的教程中效果很好,但是当我将其添加到网站时没有任何反应,我做错了什么?

它的副本 用按钮和jquery切换"data-property"

<a id='bgndVideo' class='player' data-property='{"videoURL":"https://youtu.be/KW2JUfgQct0" , "containment":".video-section", "quality":"high", "autoPlay":true, "mute":true, "opacity":1}'>bg</a> 
<a class="muteButton btn btn-default" href="#">Click me to Mute</a>
<div id="muteValueDiv"></div><br>
<div id="bgndVideoDataPropDiv"></div>
$(document).ready(function () {
      $(".muteButton").click(function () {
                $(bgndVideo).data('property').mute = !$(bgndVideo).data('property').mute;
                $('#muteValueDiv').html('After negation Mute Value is - ' + $(bgndVideo).data('property').mute);
                $('#bgndVideoDataPropDiv').html('After updating data property, data property is ' + JSON.stringify( $(bgndVideo).data('property')));
     });
});

这应该对你有用..它会更新mute属性并替换为新的mute属性。

$(document).ready(function(){    
  $(".muteButton").click( function (){
    var a= $('#bgndVideo').data('property');
            a.mute=!a.mute;
    alert($('#bgndVideo').data('property').mute);
    alert(JSON.stringify(a));
    $(".id").html('<a id="bgndVideo" class="player" data-property='+JSON.stringify(a)+'>bg</a>')
  });    
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="id">
 <a id="bgndVideo" class="player" data-property='{"videoURL": "https://youtu.be/KW2JUfgQct0", "containment": ".video-section", "quality":"high", "autoPlay":true, "mute":true, "opacity":1}'>bg</a>
</div>
<a class="muteButton" href="#">Mute</a>

https://jsfiddle.net/