简体   繁体   中英

How to select specific className with JavaScript

I have a small problem with selecting class with Javascript. Here is my part of the code:

getComputedStyle(e).className && "car" !== e.tagName && c.addInlineStyle(e, "background-color:#fff"), c.addInlineStyle(e, n)

What I am trying to achieve is select this car class with className

Can somebody try to help me with this?

You can try document.getElementsByClassName("car") to get the collection of all elements in the document with the specified class name (in this case, car), as a NodeList object. This NodeList object represents a collection of nodes. The nodes can be accessed by index numbers which start at 0.

document.querySelectorAll('.car') should work fine but if you want to use $('.car') you have to just include jquery in your html page before you write your script

<script
  src="https://code.jquery.com/jquery-3.4.1.slim.min.js"
  integrity="sha256-pasqAKBDmFT4eHoN2ndd6lN370kFiGUFyTiUHWhU7k8="
  crossorigin="anonymous"></script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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