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>
- 1 回答
- 0 關注
- 132 瀏覽
添加回答
舉報