我想在php文件中包含节点javascript代码

时间:2017-05-04 11:29:39

标签: php node.js

我有一个app.js文件,其中包含以下代码:

var embed = require('embed-video');

function go() {




   var c1 = document.getElementById('area1').value;

   var divElement = document.createElement("div");  
   divElement.id = "myDiv";  
   divElement.className = "myDivClass";  
   divElement.innerHTML = embed(c1);  
   document.body.appendChild(divElement); 
   form1.reset();
   var button = document.getElementById("remove");
   button.addEventListener("click", function() {
       divElement.parentNode.removeChild(divElement);
   });
    }

和index.php文件:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" class="hl-en not-logged-in ">
<head>
    <meta charset="UTF-8" />
    <title>Embedo</title>
      <script>

src="app.js"

    </script>
<meta name="google-translate-customization" content="293dbcc6d4a40585-2e495f68115e63cb-gd0522d114e7d11d6-e"></meta>
 </head>

 <body class="page-home" style=
 " margin-left: auto;
    margin-right: auto;
    width: 35%;background-color: #b0e0e6;">



 <form id="form1" >
        <div align="center">
            <textarea id="area1" rows="5" placeholder="embed here!"></textarea>
            <input type="button" value="add video!" onclick="go()" />
            <br />
            <input id="remove" type="button" value="start over!" onclick="remover()" />

        </div>
    </form>

</body>
</html>

当我按下添加视频按钮时,它会显示“go is not defined”。但是,我已将javascript文件包含在我的php文件中。我也完成了相应的npm安装,我的模块也存在。

2 个答案:

答案 0 :(得分:0)

这样做

 

并在app.js

之前添加jquery

答案 1 :(得分:0)

您需要将JS函数放在脚本中或为JS添加第二个文件

像这样

<script type="text/javascript" src="app.js"></script>

OR

    <script type="text/javascript">
    var embed = require('embed-video');

   function go() {




   var c1 = document.getElementById('area1').value;

   var divElement = document.createElement("div");  
   divElement.id = "myDiv";  
   divElement.className = "myDivClass";  
   divElement.innerHTML = embed(c1);  
   document.body.appendChild(divElement); 
   form1.reset();
   var button = document.getElementById("remove");
   button.addEventListener("click", function() {
       divElement.parentNode.removeChild(divElement);
   });
    }


</script>