简体   繁体   English

如何选择具有多个类的 DOM 元素?

[英]How do I select a DOM element that has multiple classes?

I am trying to select a DOM element that has these classes:我正在尝试选择具有以下类的 DOM 元素:

mq-editable-field mq-math-mode mq-field

I have tried using:我试过使用:

document.getElementsByClassName('mq-editable-field mq-math-mode mq-focused')

This is not working, is there another document function I should be using?这不起作用,我应该使用另一个文档功能吗? I am using vanilla javascript.我正在使用香草 javascript。

使用查询选择器:

document.querySelector('.mq-editable-field.mq-math-mode.mq-focused')

you can use querySelector and querySelectorAll for get multiple-element by classes您可以使用querySelectorquerySelectorAll按类获取多个元素

querySelector method : return the first element within the document which matches a specified CSS selector(s) querySelector 方法:返回文档中与指定 CSS 选择器匹配的第一个元素

let firstElement= document.querySelector('.mq-editable-field.mq-math-mode.mq-focused');

querySelectorAll() method : method returns all the elements within the document which matches the specified CSS selector(s). querySelectorAll() 方法:方法返回文档中与指定 CSS 选择器匹配的所有元素。

let elements = document.querySelectorAll('.mq-editable-field.mq-math-mode.mq-focused');

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

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