简体   繁体   English

jQuery的悬停功能不改变类

[英]Jquery Hover function not changing class

I've created a grid of pixels and want it to change to black when hovered. 我创建了一个像素网格,并希望将其悬停时更改为黑色。 The problem is that the event is not working. 问题是该事件无法正常进行。

 $(document).ready(function() { function makeGrid(k) { var size = 320 / k; for (var i = 0; i < k; i++) { $(".container").append("<div class=row></div>"); } for (var j = 0; j < k; j++) { $(".row").append("<div class=square></div>"); } $('.square').css({ 'height': size, 'width': size }); } $('.square').hover(function() { $(this).addClass(".hover"); }) $('.reset').on('click', function() { $(".container").empty(); makeGrid(16); }) $('.start').on('click', function() { var n = prompt("Set the size"); $(".container").empty(); makeGrid(n); }) }) 
 .square { border-collapse: collapse; display: inline-block; } .container { text-align: -webkit-center; position: relative; top: 50px; margin: 0 auto; border: 1px solid #000000; width: 320px; height: 320px; border-collapse: collapse; } .row { clear: both; content: ""; height: 20px; } .hover { background-color: black; } button { text-align: center; background-color: white; display: inline-block; font-size: 20px; border-radius: 2px; top: 50%; } .wrap { text-align: -webkit-center; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <div class="wrap"> <button class="clear">New Grid</button> <button class="start">Start</button> <button class="reset">Reset</button> </div> <div class="container"></div> 

Since you dynamically add your divs, you can't use .hover() and have to use .on() with mouseenter instead. 既然你动态添加的div,你不能使用.hover()并有能力使用.on()mouseenter代替。 Also when using .addClass() you just use the class' name with no period being prefixed. 同样,在使用.addClass()您只需使用类名,并且不加前缀。

 $(document).ready(function() { function makeGrid(k) { var size = 320 / k; for (var i = 0; i < k; i++) { $(".container").append("<div class=row></div>"); } for (var j = 0; j < k; j++) { $(".row").append("<div class=square></div>"); } $('.square').css({ 'height': size, 'width': size }); } $(document).on("mouseenter", ".square", function(e) { $(this).addClass("hover"); }); $('.reset').on('click', function() { $(".container").empty(); makeGrid(16); }) $('.start').on('click', function() { var n = prompt("Set the size"); $(".container").empty(); makeGrid(n); }) }) 
 .square { border-collapse: collapse; display: inline-block; } .container { text-align: -webkit-center; position: relative; top: 50px; margin: 0 auto; border: 1px solid #000000; width: 320px; height: 320px; border-collapse: collapse; } .row { clear: both; content: ""; height: 20px; } .hover { background-color: black; } button { text-align: center; background-color: white; display: inline-block; font-size: 20px; border-radius: 2px; top: 50%; } .wrap { text-align: -webkit-center; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <div class="wrap"> <button class="clear">New Grid</button> <button class="start">Start</button> <button class="reset">Reset</button> </div> <div class="container"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM