Uncaught SyntaxError: Unexpected end of input for <script> </script>

Uncaught SyntaxError: Unexpected end of input for <script> </script>

我正在使用以下 JavaScript / html 在 Chrome 的视频中播放。

控制台记录“未捕获的语法错误:输入意外结束”。

我不明白我做错了什么。我的错误是什么?

谢谢!

<html>
    <head><meta name="viewport">
        <style>
            video
            {
                position: relative;
            }
        </style>
    </head>
    
    <body>

        <video ontouchstart="playPause()" controls="controls" poster="" video width="100%" height="auto" autoplay id="vid">
            <source id="somefileID" src="file:///d:/dev/videos/Das%20Krokodil-Lied.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"' />
        </video>

        <script>
        function playPause(e) 
        {
            if (!document.getElementById("myVideo")) 
            {
                alert("Element does not exist. Let's create it.");
            }
            var myVideo = document.getElementById("vid");
            if (myVideo = null) 
            { 
                alert('video is null!');
            }
            else
            {
             if (myVideo.paused)
             {
                 myVideo.play();
             }
             else
             {
                 myVideo.pause();
             }
        }
        </script>

    </body>
</html>

您有几个问题:

  1. 您错过了代码中的最后一个大括号。使用良好的制表符结构可以帮助您更轻松地看到缺少大括号的位置。
  2. 您使用了赋值 = 而不是 ==

在下面寻找 ****:

function playPause(e) 
{
    if (!document.getElementById("myVideo")) 
    {
        alert("Element does not exist. Let's create it.");
    }
    var myVideo = document.getElementById("vid");
    if (myVideo == null) // **** You probably mean == ****
    { 
        alert('video is null!');
    }
    else
    {
        if (myVideo.paused)
        {
            myVideo.play();
        }
        else
        {
            myVideo.pause();
        }
    }

} // **** MISSING ****