Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
Detectar una palabra y agregar Clase al cuerpo

El jQuery a continuación detecta la palabra first y third en cualquier texto <p> y le agrega un fondo amarillo o rojo.

 $(document).ready(function(){ $("p:contains(first)").css("background-color", "yellow"); $("p:contains(third)").css("background-color", "red"); }); <p>I'm the first sentence.</p> <p>I'm the second sentence.</p> <p>I'm the third sentence.</p> <p>I'm the fourth sentence.</p>

JSFiddle: https://jsfiddle.net/3p0nmw4f/1/

Sin embargo, en lugar de resaltar el texto, quiero addClass al cuerpo del documento html de esta manera:

 <body class="first">

o

 <body class="third">

Sé que podemos lograr esto usando $(document.body).addClass('first'); pero no puedo armarlo.

Por favor ayuda.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puede buscar el elemento HTML del cuerpo más cercano. Puedes hacer esto haciendo lo siguiente:

 $(document).ready(function(){ $("p:contains(first)").closest('body').addClass('first'); $("p:contains(third)").closest('body').addClass('third'); });

Otra posibilidad sería simplemente buscar el elemento del cuerpo de la siguiente manera:

 $(document).ready(function(){ if ($("p:contains(first)")) $("body").addClass("first") if ($("p:contains(third)")) $("body").addClass("third") });

Fuente: https://jsfiddle.net/fk05dabw/

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!