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

200
Views
Después de hacer clic en el botón, la alerta no aparece

Estoy haciendo un proyecto en papel-piedra-tijera que tiene interfaz de usuario. Entonces, tengo cuatro botones, Iniciar juego, piedra, papel y tijera . Quiero que si hago clic en el botón Iniciar juego, aparecerá una alerta ("Elige piedra, papel o tijera"). Ahora mismo si hago clic en el juego de inicio. No pasa nada. ¿No tengo ni idea de porqué? A continuación se muestra mi código:

-------- Código HTML ---------

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Project Rock Paper Scissors</title> <script src = "javascript.js"> </script> </head> <body> <div class="btn-group"> <button id="startgame"> Start game </button> <button id="rock"> Rock </button> <button id="paper"> Paper </button> <button id="scissor"> Scissor </button> </div> </body> </html>

-------- JavaScript ----------

 const startGame = document.querySelector('#startgame'); if (startGame) { startGame.addEventListener('click', () => { alert("Choose Rock, Paper or Scissor"); }) } const rock = document.querySelector('#rock'); const paper = document.querySelector('#paper'); const scissor = document.querySelector('#scissor');
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

En su código, startGame devuelve nulo

Método 1

En su código html, llame al código javascript al final, antes de cerrar la etiqueta <body>

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Project Rock Paper Scissors</title> </head> <body> <div class="btn-group"> <button id="startgame"> Start game </button> <button id="rock"> Rock </button> <button id="paper"> Paper </button> <button id="scissor"> Scissor </button> </div> <script src = "javascript.js"> </script> </body> </html>

Método 2

Agregue el atributo diferido a su etiqueta de secuencia de comandos

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Project Rock Paper Scissors</title> <script src = "javascript.js" defer> </script> </head> <body> <div class="btn-group"> <button id="startgame"> Start game </button> <button id="rock"> Rock </button> <button id="paper"> Paper </button> <button id="scissor"> Scissor </button> </div> </body> </html>

Puede consultar esta pregunta para obtener más información.

about 4 years ago · Santiago Trujillo 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!