亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 JavaScript 從數據庫播放視頻

使用 JavaScript 從數據庫播放視頻

PHP
慕雪6442864 2023-04-02 14:55:16
我正在制作一個視頻共享網站,允許用戶登錄和上傳視頻。一切正常!問題是用js播放視頻不行;相反,只播放第一個視頻。這是我檢索視頻的代碼 (php/html)    <?php    include("conn.php");    $sql = mysqli_query($con, "SELECT * FROM uploads ORDER BY file_id DESC");    while ($row = mysqli_fetch_array($sql)) {      $path = $row['path'];      $caption = $row['caption'];  ?><div class="responsive">  <div class="gallery">    <a href="#" onclick="play()">      <video  width="600" height="400" id="video">        <source src="store/vids/<?php echo $path;?>" type="video/mp4">      </video>    </a>    <div class="desc"><br><?php echo $caption?> <span class="badge badge-pill badge-primary">Trending</span></div>  </div></div><?php  } ?>腳本看起來像:var video = document.getElementById("video"); function play() {   if (video.paused)     video.play();   else     video.pause(); } 這個東西只播放我的數據庫表中的第一個視頻。歡迎任何答案,包括可以使工作更輕松的 js 庫。
查看完整描述

1 回答

?
小怪獸愛吃肉

TA貢獻1852條經驗 獲得超1個贊

您可以在不需要 ID 屬性的情況下完成特定視頻的定位,而是使用querySelectorAll和querySelector(您也可以使用父/子/兄弟遍歷技術)


如果您創建一個nodelistusing ,querySelectorAll您可以在外部分配事件偵聽器而不是使用<a onclick='play()'>etc ,這是首選方法。該click事件已注冊為您可以發出另一個查詢以查找使用this的子元素thisquerySelector


<?php

    include("conn.php");

    $sql = mysqli_query($con, "SELECT * FROM uploads ORDER BY file_id DESC");

    while ($row = mysqli_fetch_array($sql)) {

        $path = $row['path'];

        $caption = $row['caption'];


?>


<div class="responsive">

  <div class="gallery">

    <a href="#">

      <video  width="600" height="400">

        <source src="store/vids/<?php echo $path;?>" type="video/mp4">

      </video>

    </a>

    <div class="desc"><br><?php echo $caption?> <span class="badge badge-pill badge-primary">Trending</span></div>

  </div>

</div>


<?php 

    }//close while loop

?>



<script>

    Array.from( document.querySelectorAll('.gallery > a') ).forEach( a=>{

        a.addEventListener('click',function(e){

            var video=this.querySelector('video');


            if( video.paused )video.play();

            else video.pause();


            // find other videos that are NOT the one just activated and pause them

            Array.from( document.querySelectorAll('.gallery > a > video') ).forEach( el=>{

                if( el!=video )el.pause()

            })

        })

    })

</script>


查看完整回答
反對 回復 2023-04-02
  • 1 回答
  • 0 關注
  • 132 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號