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

474
Vistas
¿Cómo obtener Element by XPATH en JavaScript?

Tengo un código muy básico. Simplemente cambia el estilo del botón al hacer clic. Funciona bien cuando se usa getElementById pero quiero hacerlo de la manera XPATH . Soy nuevo en JavaScript. ¿Qué estoy haciendo mal y cómo puedo lograr esto?

El código:

 <!DOCTYPE html> <html> <body> <p id="demo">Click the button to change the layout of this paragraph</p> <button onclick="myFunction()">Click Me!</button> <script> function myFunction() { let x = document.getElementByXpath("//html[1]/body[1]/button[1]"); x.style.fontSize = "25px"; x.style.color = "red"; } </script> </body> </html>
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Mire document.evaluate()

 function getElementByXpath(path) { return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; } function myFunction() { let x = getElementByXpath("//html[1]/body[1]/button[1]"); x.style.fontSize = "25px"; x.style.color = "red"; }
 <p id="demo">Click the button to change the layout of this paragraph</p> <button onclick="myFunction()">Click Me!</button>

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