简体   繁体   English

按id获取元素,然后按class属性获取

[英]get element by id and then class attribute

I am trying to get an elements class name. 我试图获得一个元素类名。 First I find the element by its id and then I tried to get the class attribute doing the following. 首先,我通过其ID查找元素,然后尝试执行以下操作以获取class属性。 My results return undefined. 我的结果返回未定义。 How can I get the text from the class attribute? 如何从class属性获取文本? Which would be "not-checked-in". 这将是“未签入”。

html html

<div id="last-check-in" class="not-checked-in"></div>

javascript javascript

var checkedin;
checkedin = document.getElementById("last-check-in");
console.log(checkedin.class);

Instead of simply class , use className : 使用className代替简单的class

var checkedin = document.getElementById("last-check-in");
console.log(checkedin.className);

You may use: getAttribute("class") 您可以使用: getAttribute("class")

var checkedin;
checkedin = document.getElementById("last-check-in");
console.log(checkedin.getAttribute("class"));

DEMO 演示

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

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