Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

64
Vistas
Exclude some areas on filtering the DOM with jQuery

I'm filtering the entire DOM to replace some strings with other strings.

For this I wrote the following code:

$('body :not(script)').contents().filter(function() {
  return this.nodeType === 3;
}).replaceWith(function() {
  return this.nodeValue.replace('T 0','T  0');
});

How can I exclude some areas? For example I want the DIV with the class .example not to be considered.

I tried the following:

$('body :not(script):not(.example)').contents().filter(function() {
  return this.nodeType === 3;
}).replaceWith(function() {
  return this.nodeValue.replace('T 0','T  0');
});

Why does this not seem to work?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Right now, you're telling jQuery to exclude the node .example, which has children nodes, that WILL be included.

In your :not() selector add an * after the class. This will tell jQuery to exclude all nodes underneath the parent node.

In CSS the * is a universal selector

The CSS universal selector (*) matches elements of any type.

Here's a working version: (Please note that I changed the   to - to visually show the change)

$( "input:not(:checked) + span" ).css( "background-color", "yellow" );
$( "input").attr( "disabled", "disabled" );

$('body :not(script):not(div.example *)').contents().filter(function() {
  return this.nodeType === 3;
}).replaceWith(function() {
  return this.nodeValue.replace('T 0','T--0');
});
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>not demo</title>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>

<div class="example">
  <p>Remains the same:</p>
  <h1>T 0</h1>
</div>

<div>
  <p>Changes:</p>
  <h1>T 0</h1>
</div>
 
</body>
</html>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda