简体   繁体   English

使用jQuery按类别选择子div

[英]Selecting child divs by class using jQuery

I'm trying to create a game of memory using Javascript/jQuery. 我正在尝试使用Javascript / jQuery创建内存游戏。 Basically, a random array is generated at the beginning of each game, and the user's task is to match sets of letters. 基本上,在每个游戏开始时都会生成一个随机数组,用户的任务是匹配字母集。 I'm having trouble selecting individual letters from the array. 我无法从数组中选择单个字母。

Here's a link to a working example on jsfiddle, but the relevant parts are below: 这是 jsfiddle上工作示例的链接 ,但相关部分如下:

Here's the html that the array is inserted into: 这是将数组插入其中的html:

<div id="game"></div>

Right now, I'm just trying to implement a hover function that changes the background color of the letter. 现在,我只是试图实现一个悬停功能,以更改字母的背景颜色。 Here's the CSS: 这是CSS:

.hover
{
  background-color: #A6977B;
}

Here's the Javascript that generates the array: 这是生成数组的Javascript:

function generateBoxes(gameSize, lettersSize) { 
  var currentLetter;
  letters = randomizeArray(lettersSize);
  rows = gameSize/columns; 
  for (var i=0; i<rows; i++) {
    var row = $("<div class='row'></div>");
    $("#game").append(row);
    for (var n=0; n<columns; n++) {
      currentLetter = letters[5*i + n];
      row.append("<div class='column displayNone' id = 'r" + i + "c" + n + "'>" + currentLetter + "</div>");
    }
  }

};

Here's the Javascript for the hover function: 这是悬停功能的Javascript:

  $("#game").hover(function() {
    $(this).toggleClass("hover");
  }); 

This Javascript code for the hover function works to select all the letters within the array. 悬停功能的此Javascript代码用于选择数组中的所有字母。 I only want to select one at a time, however. 我只想一次选择一个。 I've tried replacing #game with: 我尝试将#game替换为:

  • #game div
  • #game .row .column
  • .column

None of those options work to select just one letter (in fact, when I use anything besides just #game , the hover function isn't called at all). 这些选项都不能选择一个字母(实际上,当我仅使用#game ,根本不会调用悬停功能)。

What is the correct way to select child divs by class here? 在这里按班级选择子div的正确方法是什么?

Edit: Here is the generated html: 编辑:这是生成的html:

<div id="game"> 
  <div class="row">
    <div class="column displayNone" id="r0c0">E</div>
    <div class="column displayNone" id="r0c1">D</div>
    <div class="column displayNone" id="r0c2">E</div>
    <div class="column displayNone" id="r0c3">D</div>
    <div class="column displayNone" id="r0c4">B</div>
  </div>
  <div class="row">
    <div class="column displayNone" id="r1c0">A</div>
    <div class="column displayNone" id="r1c1">C</div>
    <div class="column displayNone" id="r1c2">C</div>
    <div class="column displayNone" id="r1c3">A</div>
    <div class="column displayNone" id="r1c4">B</div>
  </div>
</div>

There are two ways to do this: 有两种方法可以做到这一点:

(1) All CSS (1)所有CSS

div#game div:hover {
  background-color: #A6977B;
}

(2) Using Jquery (2)使用jQuery

$('#game div').hover(function(){
  $('#game div').removeClass('hover');
  $(this).addClass('hover');
  return false;
})

Since you're generating your letter divs dynamically, you'll want to bind the mouseenter and mouseleave events to the divs using jQuery's .on() function. 由于要动态生成字母div,因此需要使用jQuery的.on()函数将mouseenter和mouseleave事件绑定到div。 Try this: 尝试这个:

$('#game').on('mouseenter', 'div.column', function() {
    $(this).toggleClass("hover")
}).on('mouseleave', 'div.column', function() {
    $(this).toggleClass("hover")
});

您可以使用伪类并在ID上进行选择:

$("#game").find("div[id=r0c0]")

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

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