Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

370
Visualizações
div:contains not a valid selector

I'm following this example and typed $( "div:contains('John')" ).css( "text-decoration", "underline" ); , but got an exception:

VM23376:1 Uncaught DOMException: Failed to execute '$' on 'CommandLineAPI': 'div:contains('John')' is not a valid selector.

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You can use JS vanilla to do this :

function contains(selector, text) {
  var elements = document.querySelectorAll(selector);
  return Array.from(elements).filter(function(element) {
    return RegExp(text).test(element.textContent);
  });
}

// Run Code
let found = contains('span', 'Find');
// You can also search with regex
found = contains('span', 'Find\\s*[A-Za-z]+');

for (let i = 0; i < found.length; i++) {
  let elem = found[i];
  elem.style.backgroundColor = 'red';
}
<p>Some text where you have span tag that said <span>Find Me</span></p>

about 4 years ago · Santiago Trujillo Relatório

0

I guess it was a copy-paste issue. OP typed 'div:contains('John')' instead of "div:contains('John')". Properly copy-pasted code works without any problem.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>contains demo</title>
  <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
</head>
<body>
 
<div>John Resig</div>
<div>George Martin</div>
<div>Malcom John Sinclair</div>
<div>J. Ohn</div>
 
<script>
$( "div:contains('John')" ).css( "text-decoration", "underline" );
</script>
 
</body>
</html>

about 4 years ago · Santiago Trujillo Relatório

0

function '$' is unknow. You probably trying to execute this code in place where jQuery library wasn't loaded.

$( "div:contains('John')" ).css( "text-decoration", "underline" );
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
John
</div>

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda