如果班级列表包含多个特定班级 [英] If classlist contains more than one specific class

查看:37
本文介绍了如果班级列表包含多个特定班级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个函数来触发,如果元素 recordplayerstick 包含 pinplace pinsongplay 类.我当前拥有的代码返回语法错误.正确的方法是什么?

I need a function to trigger if the element recordplayerstick contains either the pinplace or pinsongplay class. The code I currently have returns a syntax error. What is the correct way to do this?

if (document.getElementById('recordplayerstick').classList.contains('pinplace pinsongplay')) {
    removepin();
}

推荐答案

如果要使用classList,则必须做两项检查.

You are going to have to do two checks if you are going to use classList.

function removepin() {
  console.log("yep");
}
var cList = document.getElementById('recordplayerstick').classList;
if (
  cList.contains('pinplace') ||
  cList.contains('pinsongplay')) {
  removepin();
}

<div id="recordplayerstick" class="pinplace pinsongplay"></div>

这篇关于如果班级列表包含多个特定班级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆