检查是否将类添加到元素

时间:2018-05-04 17:19:04

标签: javascript

我已经编码以验证是否使用javascript添加了p中的类。



function ll_ctq() {

  var check_ll_ctq = document.getElementById("ll_ctq"),
    classes = ['red_cross'];

  if (check_ll_ctq == "red_cross") {
    alert("Yes");
  } else {
    alert("No");
  }
}

<body onload="ll_ctq()">
  <p id="ll_ctq" class="red_cross">Hello</p>
</body>
&#13;
&#13;
&#13;

但它总是提醒 No

2 个答案:

答案 0 :(得分:3)

您需要使用 classList 来获取"react-native": "0.55.2"元素的类列表,然后调用check_ll_ctq来检查列表是否包含类{ {1}},如:

.contains

&#13;
&#13;
red_cross
&#13;
if ( check_ll_ctq.classList.contains("red_cross") ) {
    alert("Yes");
} else {
    alert("No");
}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您需要将元素作为变量:

ret

然后检查它的className属性以查看它是否等效:

The "sonata.seo.page" service or alias has been removed or inlined when the container was compiled. You should either make it public, or stop using the container directly and use dependency injection instead.
相关问题