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

128
Views
Reproducir sonido al hacer clic (en cualquier lugar)

Estoy tratando de crear una función que cuando hace clic en cualquier parte de la página se reproduce un sonido. Solo puedo encontrar información sobre cómo hacer esto para los botones. si me puedes ayudar seria increible

html css y js

Gracias

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

0

Este script vincula un evento de clic a cualquier elemento del cuerpo; al hacer clic, se reproducirá el sonido indicado en la etiqueta de audio.

 <!doctype html> <html> <head> <title>Play audio</title> </head> <body onload="addEvent()"> <script> function addEvent(){ document.body.addEventListener("click", play) } function play() { var audio = document.getElementById("audio"); audio.play(); } </script> <h1>click any element on the body to play sound </h1> <audio id="audio" src="https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.mp3"></audio> </body> </html>

about 4 years ago · Juan Pablo Isaza Report

0

¿Por qué no usar simplemente el evento onClick en el cuerpo?

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body onclick="playAudio()" style="height: 1000px;"> <p>CLICK</p> <audio id="audio" src="https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3"></audio> <script> function playAudio() { var audio = document.getElementById("audio"); audio.play(); } </script> </body> </html>

about 4 years ago · Juan Pablo Isaza Report

0

solo debe manejar el evento onclick.

 <html lang="en"> <head> <meta charset="UTF-8"> </head> <body> </body> <script> var should_play=true window.onclick = () => { if (should_play){ should_play=!should_play let soundSource = "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3"; let sound = new Audio(soundSource); sound.play(); sound.onended = () => { should_play=true } } } </script> </html>

Se ha definido la variable should_play para no volver a reproducir la canción cuando se está reproduciendo.

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!