用按钮和jquery切换“data-property”
Toggle 'data-property' with a button & jquery
我有一个全屏YouTube视频作为我正在开发的网站的背景,我正在尝试添加一个带有按钮的静音/取消静音切换。这就是我拉视频的方式:
<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>
我知道我想如何运行切换的逻辑,当按钮单击时,我希望数据属性更改,因此如果其数据属性为 mute:true onClick,我希望它更改为静音:false,然后在再次单击时返回。
这是我尝试过的——
$(document).ready(function(){
$(".muteButton").click( function (){
$(this).data('mute', !$(this).data('mute'));
});
});
带有锚标记 -
<a class="muteButton" href="#">Mute</a>
我已经查找了如何做到这一点但没有运气,我从未写过jquery,所以我迷路了,这是一件容易的事情吗?
谢谢
bgndVideo 的数据属性不是有效的 json 语法。 因此,它被视为字符串。
<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')));
});
});
请参考 jsfiddle: jsfiddle.net/u89oexea
相关文章:
- 角度:在ng重复上切换图像
- $(this).prop('property') vs. this.property
- Ember Data DS.Model's set函数不起作用
- 根据某些条件在视图之间切换
- 触摸移动时切换到新元素
- Extending Ext.data.NodeInterface
- 单击时切换两个图像
- jquery在表单之间切换
- Nodejs API控制器,用于在API之间切换
- 用按钮和jquery切换“data-property”
- Angular UI-Grid TypeError: Cannot set property 'data'
- d3.js - Uncaught TypeError: Cannot read property 'data
- JQuery -根据类名和数据属性切换选定行.(data-tag-id)
- react:不能读取property 'data'的定义
- 无法读取property 'data'未定义的# 39;nodeJS
- 未捕获的TypeError: Cannot read property 'data'null的数组
- 未捕获的TypeError: Cannot read property 'data'jquery Jta
- interceptors TypeError: Cannot read property 'data'
- 获取TypeError: Cannot read property 'data'当试图包含我用Angul
- 切换版本的jQuery,现在得到Uncaught TypeError: Cannot read property