我试图在javascript中做一个简单的按钮,做两件事

如何解决我试图在javascript中做一个简单的按钮,做两件事

我正在寻找一种同时具有两个独立的操作/功能方法

打开和关闭按钮

一个会影响我的视频音量(打开和关闭)的按钮

我有两个脚本,但是不知道如何将两者合而为一。

这是2个脚本:

<button onclick="toggleMute();">Volume</button>
                    
<input type="button" id="1" value="ON" style="color:blue" onclick="toggle(this);">
                    
<script type="text/javascript"> 
    function toggle(button)
    {
      if(document.getElementById("1").value=="OFF"){
       document.getElementById("1").value="ON";}
    
      else if(document.getElementById("1").value=="ON"){
       document.getElementById("1").value="OFF";}
    }    
</script>
                    
<script>
    function toggleMute() {
        var video=document.getElementById("theplayer")
            if(video.muted){video.muted = false;} 
            else {video.muted = true;}
    };
</script> 

这是我的视频:)

<video autoplay="autoplay" loop="" muted="muted" webkit-playsinline="" playsinline="" class="video" id="theplayer" type="video/mp4" width="100%" height="auto" style="display:block; margin:0 ; padding: 0;">
                  <source src="<?PHP echo get_post_meta( get_the_ID(),'content-video',true ); ?>" type="video/mp4">
</video>

非常感谢

解决方法

我在这里重构了您的代码

document.querySelector("input").onclick = function() {
  var states = {"OFF": "ON","ON": "OFF"};
  this.value = states[this.value];
}              

document.querySelector("button").onclick = function() {
  var video = document.getElementById("theplayer");
  video.muted = !video.muted;
}
<button>Volume</button>          
<input type="button" value="ON" style="color:blue">
<br>
<video src="https://res.cloudinary.com/saymoinsam/video/upload/v1541946026/PiratesOfTheCaribbean.mp4" id="theplayer" autoplay="autoplay" loop="" muted="muted">
</video>

这是仅使用复选框的另一种实现,因为它是启用或禁用的所有输入,因此它是更正确的输入元素

document.querySelector("#volume-switcher").onchange = function() {
  document.querySelector("#theplayer").muted = !this.checked;
}              
#volume-switcher {
  display: none;
}
#switcher-container {
  display: inline-block;
  width: 40px;
  height: 20px;
  border-radius: 15px;
  background-color: #eee;
  position: relative;
  box-shadow: 1px 1px 2px #ccc;
  cursor: pointer;
}

#switcher-container:after {
  content: "";
  background-color: white;
  position: absolute;
  top: -2px;
  left: 0;
  width: 20px;
  height: 24px;
  border-radius: 50%;
  box-shadow: 1px 1px 2px #666666;
}

#volume-switcher:checked + #switcher-container {
  background-color: #9999ff;
}

#volume-switcher:checked + #switcher-container:after {
  left: 20px;
}
<input type="checkbox" id="volume-switcher">
<label id="switcher-container" for="volume-switcher"></label>
<br>
<video src="https://res.cloudinary.com/saymoinsam/video/upload/v1541946026/PiratesOfTheCaribbean.mp4" id="theplayer" autoplay="autoplay" loop="" muted="muted">
</video>

,

感谢大家的帮助,以及您对javascript中我的lvl的理解:)) @saymoinsam确实很有帮助,我完成的按钮是help和your:

查看此Codepen上的所有结果(含CSS):[https://codepen.io/quentindigital/pen/oNxwNrv]

app-csv-header-selection:not(:first) {
    margin-left: 2rem;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?