javascript - 如何在js中创建2个按钮播放,暂停
问题描述
我在卡上有 2 个按钮,如下所示。
<div class = "card">
<button class ="play"> Play </button>
<button class ="pause"> PAuse </button>
</div>
当我按下PLay
按钮时,Pause
应该启用。Play
按钮应该被禁用。
按下后Play
,如果我按下Pause
按钮,Play
按钮应该是Enabled
,Pause
按钮应该是disabled
。
一件事,我们不能直接按下Pause
按钮,而不按下Play
按钮。
我如何编写代码来显示条件Play
和Pause
JS。
有人可以帮忙吗?
谢谢
解决方案
默认设置disabled="true"
按钮.pause
并继续设置click
两个按钮的事件
$(".play").on("click", function(){
$(this).prop("disabled", true );
$(".pause").prop("disabled", false );
})
$(".pause").on("click", function(){
$(this).prop("disabled", true );
$(".play").prop("disabled", false );
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class = "card">
<button class ="play"> Play </button>
<button class ="pause" disabled="true"> PAuse </button>
</div>
如果您对 DOM 没有控制权并且无法添加disabled="true"
for .pause
,则可以$(".pause").prop("disabled", true )
在定义事件之前使用
$(".pause").prop("disabled", true );
$(".play").on("click", function(){
$(this).prop("disabled", true );
$(".pause").prop("disabled", false );
})
$(".pause").on("click", function(){
$(this).prop("disabled", true );
$(".play").prop("disabled", false );
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class = "card">
<button class ="play"> Play </button>
<button class ="pause"> PAuse </button>
</div>
推荐阅读
- python - 我试图添加一个将新用户名和密码输入我的字典的函数,但它无法识别新信息
- sql-server-2008 - SSIS 2008:尝试重命名文件时路径中有非法字符
- java - java中的串口通信
- python - python 使用 file.write 从写入的文件中删除最后一行
- ansible - 在 Ansible 变量中转义双引号和大括号
- excel - Excel - VBA - 创建每个主键的唯一代码组合
- php - Twig AND 运算符与等于
- r - 我如何将 purrr 的地图与带有变量的用户定义函数一起使用
- javascript - 通过 Google Apps 脚本中的 GID 获取特定工作表
- datepicker - 在日期时间选择器中,我想隐藏默认的结算日期