I need your help, because I want to make a span
to play music. I made a Javascript code and tried to edit the HTML, but the span
is still not clickable. I am not sure that this would be the best solution for what I want.
This is my HTML:
<div class="col-md-4">
<div class="service-box">
<div class="service-ico">
<span id="hang1" class="ico-circle"><i class="ion-play"></i></span>
</div>
<div class="service-content">
<h2 class="s-title">Magyar hangminták</h2>
<p class="s-description text-center">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Magni adipisci eaque autem fugiat! Quia,
provident vitae! Magni
tempora perferendis eum non provident.
</p>
</div>
</div>
</div>
And this is the JS:
var hang1 = new Audio();
hang1.src = "hang1.mp3"
$("#myspan").click(function(){
hang1.play()
})
Assuming you want it to play the music upon a click; you need to create the audio object; and hide it
<audio id="audio" controls style="display:none">
<source src="\\youraudio path.mp3" type="audio/mpeg">
</audio>
Then use a EventListener
for the right key to be pressed:
document.addEventListener('keydown', function(e) {
if (e.keyCode == 13) {
document.getElementById('audio').play();
}
});
in this example 13
being enter key.
Hope this help.
Add to html <audio>
tag and the sources of music in it.
HTML
<audio id="myAudio">
<source src="horse.ogg" type="audio/ogg">
<source src="horse.mp3" type="audio/mpeg">
</audio>
<div class="col-md-4">
<div class="service-box">
<div class="service-ico">
<button onclick="playAudio()" type="button"><span id="hang1" class="ico-
circle"><i class="ion-play"></i></span></button>
</div>
<div class="service-content">
<h2 class="s-title">Magyar hangminták</h2>
<p class="s-description text-center">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Magni adipisci eaque
autem fugiat! Quia,
provident vitae! Magni
tempora perferendis eum non provident.
</p>
</div>
</div>
JS
var x = document.getElementById("myAudio");
function playAudio() {
x.play();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.