Pick element by css selector

Asked

Viewed 26 times

-2

I have a div where I want to take it by function document.querySelector but I can’t

The div:

<div class=" f:.9 m-b:.4 m-t:.5 d:i-b ">Conteúdo</div>

I tried to make document.querySelector('div.f:.9 m-b:.4 m-t:.5 d:i-b') but I couldn’t

Someone knows how to do it?

1 answer

1


Try this:

// Exemplo
let queryScape = document.querySelector(`.f\\:\\.9.m-b\\:\\.4.m-t\\:\\.5.d\\:i-b`);

queryScape.style.background = 'red';

// Código
//document.querySelector(`.f\\:\\.9.m-b\\:\\.4.m-t\\:\\.5.d\\:i-b`);
<div class="f:.9 m-b:.4 m-t:.5 d:i-b">Conteúdo</div>

Explanation:

The characters ' : ' and ' . ' in the class conflict with normal CSS selectors for class and special attributes, like :Nth-Child or :last-Child, so when using these characters in the class name, it is necessary to "escape" the selection query

To escape, just use ' ' before the special character.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.