简体   繁体   English

我如何在刀片花括号中使用javascript变量

[英]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.

相关问题 在laravel刀片花括号内连接Javascript变量 - Concatenate Javascript variable inside laravel blade curly braces 如何控制段落中任何文本的字体粗细( <p> 使用JavaScript在大括号或大括号{}内 - How do I control the font weight of any text within a paragraph (<p> tag) within braces or curly brackets { } using JavaScript 我可以在玉花括号中将玉器变量用作对象标识符的一部分吗? - Can I use a jade variable as part of an object identifier within Angular curly braces? 如何在同一角花括号({{}})中动态呈现javascript数据和html? - How can I dynamically render both javascript data and html within the same angular curly braces ({{ }})? 我可以在javascript中使用花括号来分隔代码段吗 - Can I use curly braces in javascript to separate sections of code 如何在 JavaScript 中替换花括号? - How to substitute curly braces in JavaScript? 在花括号内连接字符串和变量 - Concatenating a string and a variable within curly braces 如何通过“ shelljs”使用大括号在单个命令中创建多个目录? - How do I use curly braces to create multiple directories in a single command via “shelljs”? Javascript在花括号内获取子字符串值 - Javascript get substring values within the curly braces 如何实际捕获JavaScript中花括号之间的内容? - How can I actually capture things between curly braces in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM