【发布时间】:2017-03-16 09:52:27
【问题描述】:
当我仅在 HTML 上测试代码时,它工作得很好,但是当我分开它时,它没有显示 -surprise - 这是我的代码:
<!DOCTYPE html>
<html>
<body>
<img id='qrcode' src=''>
<button onclick="newQR()">Gerar QRcode</button>
<script>
function newQR() {
var x = Math.floor((Math.random() * 99) + 1);
document.getElementById('qrcode').src = "https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=" + x
}
newQR()
</script>
</body>
</html>
编辑:我想将它们分开,例如,在 script.js 中包含函数 newQR,在 index.html 中包含函数,如下所示:
script.js
function newQR() {
var x = (Math.random() * 99999999999999999);
document.getElementById('qrcode').src ="https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=" + x
return
}
index.html
<!DOCTYPE html>
<html>
<body>
<button onclick="newQR()">Gerar QRcode</button>
<img = 'qrcode' />
<script type="js/javascript" src="script.js"></script>
</body>
</html>
【问题讨论】:
-
它在我的编辑器上工作。另外,在函数之后调用“newQR()”是多余的。
-
它可以工作,但我不能分开,只留下一个HTML文件,只显示按钮和将生成的图像以及带有功能代码的JS文件。
-
你创建了你的javascript文件了吗?如果是,请向我们展示该文件中的代码。
-
是的,我编辑了问题,抱歉没有更具体,
-
会不会是你忘记了图片上的“id”标签?
标签: javascript html