老师点击的是可以完成可以键盘输入没有反应

来源:1-19 自由编程

Levinson

2019-09-17 21:55:33

<!DOCTYPE html >
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQuery练习</title>
<style type="text/css">
.div1{
 float: left;
 width: 50px;
 line-height: 30px;
 background-color: lightgray;
 margin-left: 10px;
 text-align: center;
 vertical-align: middle;
}
.div2{
 width: 300px;
 height: 300px;
 border: solid gray 1px;
 margin-top: 60px;
 color: red;
}
</style>

</head>
<body>
 <h2>请选择背景颜色</h2>
 <div>
 <div id="blue" class="div1">蓝色</div>
 <div id="green" class="div1">绿色</div>
 </div>
 <div class="div2" id="bg">
 </div>
 <div>输入颜色首字母: <input type="text" name="color"></div>
 <script type="text/javascript" src="js/jquery-3.4.1.js"></script>
 <script type="text/javascript">
  alert("欢迎来到设置颜色的页面");
  $("#blue").click(function(){
   $("div.div2").css("background-color" , "blue");
   $("div.div2").html("<p>蓝色背景</p>");
  });
  $("#green").click(function(){
   $("div.div2").css("background-color" , "green" );
   $("div.div2").html("<p>绿色背景</p>").css({"font-weight" : "bold","font-style" : "italic" });
  });
  $("input[name='color']").keypress(function(event){
   console.log(event);
   if(event.keycode == 66){
    $("div.div2").html("");
   }else if(event.keycode == 71){
    $("div.div2").html("");
   }
  });
 </script>
</body>
</html>

请问哪里出错了阿?

写回答

1回答

好帮手慕小班

2019-09-18

同学你好,复制运行贴出代码输入键盘数据后,没有反应是因为没有在 $("input[name='color']").keypress(function(event){}中设置样式,修改后:

http://img.mukewang.com/climg/5d819f94091d995006510232.jpg

http://img.mukewang.com/climg/5d819fac090f0cca04280470.jpg

如果我的回答解决了你的疑惑,请采纳,祝学习愉快~

0

0 学习 · 10204 问题

查看课程