分离我的html页面的javascript代码

编程入门 行业动态 更新时间:2024-10-19 18:32:41
本文介绍了分离我的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 =api.qrserver/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 =api.qrserver/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 = "api.qrserver/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 = "api.qrserver/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代码

本文发布于:2023-10-08 01:33:09,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:页面   代码   html   javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!