<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript" src="Scripts/jQuery/book.js"></script>
<script type="text/javascript" src="Scripts/jQuery/jquery.min.js"></script>
<style type="text/css">
#book {
width:160px;
height:160px;
margin-top:15px;
border: 1px solid black;
}
</style>
<script type="text/javascript">
var book = new book("book");
function makeCode() {
var elText = document.getElementById("text");
if (!elText.value) {
alert("Input a text");
elText.focus();
return;
}
qrcode.makeCode(elText.value);
}
makeCode();
$("#text").
on("blur", function () {
makeCode();
}).
on("keydown", function (e) {
if (e.keyCode == 13) {
makeCode();
}
});
</script>
</head>
<body>
<input id="text" type="text" value="create a book" style="width:80%" /><br />
<div id="qrcode"></div>
</body>
</html>
我创建了一个基本代码,但 makeCode() 似乎没有被调用。我在“Scripts/jQuery/book.js”下添加了导入的 book.js 和 jquery.min.js。通过在输入中键入一个值,将调用 makeCode。
最佳答案
您需要确保 DOM 已准备好,否则元素可能尚未加载到其中。
$(function(){
makeCode();
$("#text").
on("blur", function () {
makeCode();
}).
on("keydown", function (e) {
if (e.keyCode == 13) {
makeCode();
}
});
});
我不知道 book
函数内部做了什么,但如果它有任何 DOM 引用,那么也需要在 DOM 准备好后调用它。
关于javascript - 导入库 jquery 的错误函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32269014/