第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

使用 JavaScript 從數(shù)據(jù)庫(kù)播放視頻

使用 JavaScript 從數(shù)據(jù)庫(kù)播放視頻

PHP
慕雪6442864 2023-04-02 14:55:16
我正在制作一個(gè)視頻共享網(wǎng)站,允許用戶登錄和上傳視頻。一切正常!問題是用js播放視頻不行;相反,只播放第一個(gè)視頻。這是我檢索視頻的代碼 (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(); } 這個(gè)東西只播放我的數(shù)據(jù)庫(kù)表中的第一個(gè)視頻。歡迎任何答案,包括可以使工作更輕松的 js 庫(kù)。
查看完整描述

1 回答

?
小怪獸愛吃肉

TA貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超1個(gè)贊

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


如果您創(chuàng)建一個(gè)nodelistusing ,querySelectorAll您可以在外部分配事件偵聽器而不是使用<a onclick='play()'>etc ,這是首選方法。該click事件已注冊(cè)為您可以發(fā)出另一個(gè)查詢以查找使用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>


查看完整回答
反對(duì) 回復(fù) 2023-04-02
  • 1 回答
  • 0 關(guān)注
  • 168 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)