This question already has answers here:
How to get elements with multiple classes
                                
                                    (7个答案)
                                
                        
                                3年前关闭。
            
                    
我有一些带有“ myImages”类的图像。当我选择其中一幅图像时,该图像类名称将更改为“ myImages selected”。现在正在尝试执行if语句,以测试是否选择了任何图像,如果没有,则返回一个警告框。看起来像这样(语法不正确):

function alertBox() {
   if document.getElementByClassName("myImages")[all elements of class myImages] contains class name ("myImages selected"):
   alert("Please select an image");
}

最佳答案

function alertBox() {
  if (document.getElementsByClassName('myImages selected').length === 0) {
    alert('Please select an image');
  }
}

08-29 00:18