javascript - How to change audio src by click using jquery? -
how change dynamically audio src using jquery?
<audio id="audio" controls="" > <source src="" type="audio/mpeg" /> </audio> <ul id="playlist"> <?php if($lists) { foreach ($lists $list) { ?> <li class="active"> <a href="music/<?php echo $list; ?>.mp3"> <?php echo $list; ?> </a> </li> <?php }}?> </ul>
you need bind click event handler a
(or delegate events parent #playlist
) , assign new src #audio source
:
$('#playlist').on('click', 'a', function(e) { e.preventdefault(); var src = $(this).attr('href'); $('#audio source').attr('src', src); });
important: remember prevent default behavior of htmlanchorelement
on click page reload.
Comments
Post a Comment