JavaScript多个音频audio标签或者多个视频video,点击其中一个播放时,其他的停止播放

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>一个页面中有多个音频audio标签,怎样在点击其中一个播放时,其他的停止播放</title>
</head>

<body>
    <audio src="http://www.ytmp3.cn/down/49366.mp3" controls></audio>
    <audio src="http://www.ytmp3.cn/down/49382.mp3" controls></audio>
    <audio src="http://www.ytmp3.cn/down/49369.mp3" controls></audio>
    <script type="text/javascript">
    // 获取所有audios
    var audios = document.getElementsByTagName("audio");
    // 暂停函数
    function pauseAll() {
        var self = this;
        [].forEach.call(audios, function(i) {
            // 将audios中其他的audio全部暂停
            i !== self && i.pause();
        })
    }
    // 给play事件绑定暂停函数
    [].forEach.call(audios, function(i) {
        i.addEventListener("play", pauseAll.bind(i));
    })
    </script>
</body>

</html>

 多个视频:

// 视频点击播放/暂停
                var videoAll = $(".J_catVideo");
                videoAll.on("click",function(){
                    var cindex = videoAll.index(this);
                    videoAll.each(function (i) {
                        var video = $(this).find('video')[0];
                        if (i == cindex) {
                            if (video.paused) {
                                $(this).find(".J_videoPic").hide();
                                video.play();
                            } else {
                                $(this).find(".J_videoPic").show();
                                video.pause();
                            }
                        }else{
                            if (!video.paused) {
                                $(this).find(".J_videoPic").show();
                                video.pause();
                            }
                        }
                    })
                })
原文地址:https://www.cnblogs.com/huanghuali/p/9335537.html