分离我的html页面的javascript代码 [英] Separating javascript code of my html page

查看:143
本文介绍了分离我的html页面的javascript代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

 <!DOCTYPE html> 
< html>
< head>
< script type =text / javascript>< / script>
< / head>
< body>
< img id ='qrcode'src =''>
< button onclick =newQR()> Gerar QRcode< / button>
< script> function newQR(){
var x = Math.floor((Math.random()* 99999999999999999)+ 1);
document.getElementById('qrcode')。src =https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=+ x +& size = [500]×[500]&安培; ECC = L&安培; QQ空间= 4和;格式= PNG;
}
< / script>
< / body>



现在我想分创建一个js文件并将脚本代码放入其中。
现在我的代码是:

html文件

 <!DOCTYPE html> 
< html>
< head>
< script type =text / javascript>< / script>
< / head>
< body>
< img id ='qrcode'src =''>
< button onclick =newQR()> Gerar QRcode< / button>
< script type =text / javascriptsrc =js / script.js>< / script>
< / body>
< / html>

javascript文件(即它在一个名为js的贴图中)

  function newQR(){
var x = Math.floor((Math.random()* 99999999999999999)+ 1);
document.getElementById('qrcode')。src =https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=+ x +& size = [500]×[500]&安培; ECC = L&安培; QQ空间= 4和;格式= PNG;
}

为什么它不再工作?
<您的JavaScript链接相对而不是绝对:

 < / p> 

; script type =text / javascriptsrc =js / script.js>< / script>

 < script type =text / javascriptsrc =/ js / scripts.js>< / script> 

通过导航到 / js /的script.js 。如果它仍然无法正常工作,请检查控制台是否有错误(右键单击>检查元素)


I have this code working:

<!DOCTYPE html>
<html>
<head>
<script type = "text/javascript"></script>
</head>
<body>
   <img id='qrcode' src=''>
   <button onclick="newQR()">Gerar QRcode</button>
   <script>  function newQR() {
   var x = Math.floor((Math.random() * 99999999999999999) + 1);
   document.getElementById('qrcode').src = "https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=" + x + "&size=[500]x[500]&ecc=L&qzone=4&format=png";
 }
   </script>
    </body> 

Now I am trying to divide creating a js file and putting the script code inside. Right now my code is:

html file

<!DOCTYPE html>
<html>
<head>
<script type = "text/javascript"></script>
</head>
<body>
  <img id='qrcode' src=''>
  <button onclick="newQR()">Gerar QRcode</button>
  <script type = "text/javascript" src = "js/script.js"></script>
</body>
</html>

javascript file (that it is inside a paste called js)

  function newQR() {
   var x = Math.floor((Math.random() * 99999999999999999) + 1);
   document.getElementById('qrcode').src = "https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=" + x + "&size=[500]x[500]&ecc=L&qzone=4&format=png";
 }

Why it is not working anymore?

解决方案

Your javascript is linked relatively instead of absolutely:

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

to

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

Verify that your JS is viewable in the browser by navigating to /js/script.js. If it's still not working, check your console for errors (right click > inspect element)

这篇关于分离我的html页面的javascript代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆