[英]How do i use a javascript variable within blade curly braces
I have a video player site and i have a "next" button which should stop the video , change the src directing to the next video and play it using javascript but I cant get around using a javascript loop to change the src. 我有一个视频播放器网站,我有一个“下一步”按钮,应停止视频,更改src指向下一个视频,并使用JavaScript播放它,但我无法使用javascript循环来改变src。
Heres my blade file 继承我的刀片文件
@extends('layouts.app')
@section('content')
<section class="player">
<center>
<video id="myvideo" class="playing-video" src="{{url('songs/'.$songs[0]->filename)}}" controls autoplay onended="nextVideo()">
</video>
<br>
<button id="next" style="background-color: transparent;border: none;color: white;" onclick="nextVideo()" type="button">
<h2><i class="fas fa-chevron-right"></i>Next</h2></button>
</center>
</section>
and heres the javascript which is not working 并且继承了无效的javascript
var i = 0;
var myVideo = document.getElementById("myvideo");
function nextVideo() {
i++;
myVideo.setAttribute("src", "http://localhost/Laravel/anime/public/songs/" + {{ $songs[i] -> filename }});
myVideo.load();
myVideo.play();
}
I know I cant use a javascript variable inside the blade curly braces directly but I am out of ideas! 我知道我不能直接在刀片花括号内使用javascript变量,但我没有想法!
Store the Laravel collection as a data point on the button: 将Laravel集合存储为按钮上的数据点:
<button id="next" data-songs="{{json_encode($songs)}}" ... etc />
Then pull the JS object out within the nextVideo() JS method and loop on it (all JS): 然后在nextVideo()JS方法中拉出JS对象并在其上循环(所有JS):
function nextVideo() {
var songs = JSON.parse( //get the data from the #next button )
//loop using the JS songs object instead of the blade {{}}
}
Try using Toggle Button 尝试使用切换按钮
<button onclick="Toggle()">Next</button>
<script>
var i =0;
function toggle(){
i=++i; // each time you press button the variable i increments value by one (inside brackets only)
if(i===1){
videoStop(); // Function to stop video
}else if(i===2){
videoNext(); // Create a function which will contain url of next video , autoplay functions
i=0; // return i value to 0 and continue using the toggle button
}
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.