javascript - 使用按键 jquery/javascript 更改一个 div 的颜色
问题描述
我想通过单击“1”键来更改一个 div 的颜色。我有一堆视频正在播放,当你点击“1”时,某个视频开始播放。我想创建一个看起来像控制面板的东西,这样当你点击“1”时,视频会打开(我在javascript中使用了按键),但当我按下“1”时也会改变我的div的颜色
HTML
</head>
<body>
<div class="intro">Press The Number Keys and Q W E To Activate Video
</div>
<div id="one">1</div>
JAVASCIPT JQUERY
$(document).ready(function() {
$(document).keypress(function(event) {
if (event.which === 49) {
$("#one").css('background-color'),("#00ffff");
}
});
});
document.onkeypress = function(e) {
console.log(e)
if (e.key === "1" ) {
var vid = document.getElementById('vid');
vid.paused ? vid.play() : vid.pause();
var vid = document.getElementById('vid').style.opacity = '1';
} else if (e.key === "2" ) {
var cami = document.getElementById('cami');
cami.paused ? cami.play() : cami.pause();
var cami = document.getElementById('cami').style.opacity = '1';
}
解决方案
像这样的东西应该能让你到达那里。只是将背景颜色更改逻辑沙盒化。
<div class="intro">Press The Number Keys and Q W E To Activate Video
</div>
<div id="changeExample">This is a test</div>
document.onkeypress = function(e) {
console.log(e)
if (e.key === "1" ) {
if( $("#changeExample").hasClass("red") ){
$("#changeExample").removeClass("red").addClass("pink").css("background-color", "pink");
}
else if( $("#changeExample").hasClass("pink") ){
$("#changeExample").removeClass("pink").addClass("red").css("background-color", "red");
}
else{
$("#changeExample").addClass("red").css("background-color", "red");
}
}
}
推荐阅读
- php - 在codeigniter中使用UNION时如何计算行数?
- netsuite - 预定脚本不更新记录
- node.js - apisauce 在 localhost 上出现网络错误
- google-colaboratory - 如何在 Google Colab 中安装 ops 库?
- google-bigquery - OWOX BI Big Query Add-on 问题
- c# - 购物车页面在 Asp.Net Core 2.0 Razor 页面中仅显示一项
- android - 最新版本的 Expo 在启动时出现错误
- angular - 角度材料:如何对每个单元格中具有多个值的表格应用排序
- php - 如何检查存储在数据库中的数组中是否存在值
- ruby - 如何使用哈希类型的参数发布请求