检查元素是否有类

5
尝试寻找一种方法,在点击一个 div 元素时显示 console.log。我正在尝试制作一个简单的游戏,如果你点击正确的方框,你将收到一条消息,告诉你赢了。
目前,我在我的代码底部遇到了困难,尤其是这部分:

function winningBox(){

 if (boxes.hasClass){

  console.log('you win');
 } else {
  console.log('you lose');
 }
}
winningBox();

我该如何让它工作?如果被点击的方框有'winning'类,那么消息应该在控制台中打印出'winning'。请看一下代码。顺便说一句,我需要用原生JavaScript完成这个任务。

//cup game
//add three cups to screen
//select li element
var button;
var boxes = document.getElementsByTagName('li');
var array = [];
console.log('working');

document.addEventListener('DOMContentLoaded', init);

function init() {
  document.addEventListener('click', winningBox);


  //shuffle li elements, and ad an id
  function test(boxes) {
    var randomBox = boxes[Math.floor(Math.random() * boxes.length)];
    array.push(randomBox);
    console.log('randombox:', randomBox);
    randomBox.classList.add('winning');

  }
  console.log(test(boxes));


  //user can click on a cup to see if correct
  function winningBox() {

    if (boxes.hasClass) {

      console.log('you win');
    } else {
      console.log('you lose');
    }
  }
  winningBox();

  //if cup is incorrect, display message
  //if correct, display won message
  //button to restart game
}
body {
  background-color: #bdc3c7;
}

.main {
  background-color: #2c3e50;
  width: 300px;
  height: 100px;
}

li {
  background-color: gray;
  width: 80px;
  height: 80px;
  margin: 10px;
  list-style-type: none;
  display: inline-block;
  position: relative;
}
<body>
  <container class="main">
    <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
    </ul>
  </container>
  <script src="main.js"></script>
</body>

3个回答

10
你可以使用Element.classList.contains函数来检查元素的类属性中是否存在指定的class值。
那么断言应该是这样的:
if (boxes.classList.contains('winning')) {
更新 正如Karl Wilbur在评论中指出的那样,boxes是一个NodeList实例。 因此,您需要将其转换为数组:
var boxes = Array.prototype.slice.apply(document.getElementsByTagName('li'));

你将能够对其进行迭代:

boxes.some(function(el) {
    return el.classList.contains('winning');
});

如果至少一个框包含类名“winning”,则应返回true。


是的,它给了我某种类型的错误... - Lucky500
那么,能否告诉我们错误信息是什么? - nnnnnn
无法读取未定义的“contains”。 - Lucky500
1
如果 boxes.classListundefined,那么你可能将 boxes 视为元素数组。你可能需要迭代 boxes - Karl Wilbur
classList.contains('class_name') 对我有用 :) - Omer

0

我建议检查每个容器(而不是先前答案中的数组):

function checkawinner(box) {
  box.addEventListener("click", function(){
   if (box.classList.contains('winning')) {
        console.log('you win');
    } else {
        console.log('you lose');
   }
  }, false);
}

for (index = 0; index < boxes.length; ++index) {
  checkawinner(boxes[index]);  
}

一支带有“警报”的笔:http://codepen.io/VsevolodTS/pen/BKBjpP

0

我认为你想要做的是:

//user can click on a cup to see if correct
function winningBox(){
    // ensure that we are not working against a cached list of elements
    var boxes = document.getElementsByTagName('li');
    for(i=0,len=boxes.length;i<len;i++) {
        var box = boxes[i];
        if (box.classList.contains('winning')){
            console.log('you win');
        } else {
            console.log('you lose');
        }
    );
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接