javascript - 添加脚本代码到Wordpress

标签 javascript html wordpress

我在 W3 html 编辑器中尝试了以下代码:

主要目标:
(该代码几乎可以播放 mp4 视频,并允许用户通过单击视频屏幕来暂停/播放 - 不显示任何控件)它在 html 文件中运行良好。

我想将此代码添加到我的 WordPress 网站,但是 <script>部分不起作用。我访问了 WordPress 帮助部分,尝试将 Javascript 合并到我的网站中。有些说明并不明显遵循。如果有人能给我一些逐步的方法来使下面的代码在 Wordpress 中工作,我将非常感激。

我使用的是 WordPress 版本 4.3.1

<!DOCTYPE html> 
<html> 
<body>
<! Within the WordPress text editor, there's no need for the <html> & <body> tags >

<video id="myVideo" width="320" height="176" autoplay>
  <source src="http://techslides.com/demos/sample-videos/small.mp4" type="video/mp4">
  Your browser does not support HTML5 video.
</video>

<script type="text/javascript"> 

var vid = document.getElementById("myVideo"); 
var isPlaying = true;

vid.onclick = function playVid() {

    if (isPlaying == false){
       myVideo.play(); 
       isPlaying = true;}

    else if (isPlaying == true){
       myVideo.pause(); 
       isPlaying = false; }
} 
</script> 

<! Within the WordPress text editor, there's no need for the <html> & <body> tags >
</body> 
</html>

最佳答案

var vid = document.getElementById("myVideo");

 if (isPlaying == false){
       myVideo.play(); 
       isPlaying = true;}

    else if (isPlaying == true){
       myVideo.pause(); 
       isPlaying = false; }

您的变量是 vid 并且您正在使用 myVideo 调用 play()pause()是一个 ID

关于javascript - 添加脚本代码到Wordpress,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34160813/

相关文章:

jquery - 全年查看日历?

html - div 之后的 div,其位置在 css 中是固定的

php - WPDB 不工作,SQL 查询在 phpmyadmin 中工作

php - 编辑 WordPress 主题以类似于基于相同主题的另一个站点

php - 从 URL 参数自动填充输入表单字段

javascript - 如何在 MongoDB 中修改动态添加的文档字段

javascript - 样式化组件在重新渲染后不会更新 <div>

javascript - 对调用函数的每个变量进行空检查?

javascript - 如何在 Javascript 中对数组对象使用 forEach 方法或任何数组方法?

javascript - 如何以编程方式访问 webkit 引擎中的样式表 (Chrome/Safari)