<!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。
答案 0 :(得分:1)
您需要确保DOM已准备就绪,否则可能无法将元素加载到其中。
$(function(){
makeCode();
$("#text").
on("blur", function () {
makeCode();
}).
on("keydown", function (e) {
if (e.keyCode == 13) {
makeCode();
}
});
});
我不知道book
函数在内部做了什么,但是如果它有任何DOM引用,那么在DOM准备好之后也需要调用它。
答案 1 :(得分:0)
Javascript属于</body>
之前的页面底部,你的jQuery命令应该在dom ready包装内(虽然在页面底部不是严格要求的)。
<!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>
<style type="text/css">
#book {
width:160px;
height:160px;
margin-top:15px;
border: 1px solid black;
}
</style>
</head>
<body>
<input id="text" type="text" value="create a book" style="width:80%" /><br />
<div id="qrcode"></div>
<!-- javascript at the end, and load jquery first -->
<script type="text/javascript" src="Scripts/jQuery/jquery.min.js"></script>
<script type="text/javascript" src="Scripts/jQuery/book.js"></script>
<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();
$(function(){
$("#text").
on("blur", function () {
makeCode();
}).
on("keydown", function (e) {
if (e.keyCode == 13) {
makeCode();
}
});
});
</script>
</body>
</html>
答案 2 :(得分:0)
jQuery DOM必须准备就绪..将文档就绪函数想象成一个自动执行的函数,在页面元素加载后触发。
将您的javascript放在 document.ready 代码中,如下所示......
$(document).ready(function() {
//do jQuery stuff when DOM is ready
});
所以,举个例子:
<script type="text/javascript">
$(document).ready(function() {
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>
答案 3 :(得分:0)
谢谢大家的帮助!!! 问题是:
1)确保DOM准备就绪
class PraxSliderCell : NSSliderCell {
var lastSliderValue: Double = -1
override func closestTickMarkValueToValue(value: Double) -> Double {
var proposedValue = super.closestTickMarkValueToValue(value)
if lastSliderValue == -1 { lastSliderValue = proposedValue }
else {
let tickDifference = abs(lastSliderValue - proposedValue)
let isTurningUp = proposedValue > lastSliderValue
if tickDifference > Double(numberOfTickMarks / 2) {
proposedValue = isTurningUp ? 0.0 : maxValue }
print (NSString(format: "value: %.2f gap: %.2f", proposedValue, tickDifference))
lastSliderValue = proposedValue
}
return proposedValue
}
override func rectOfTickMarkAtIndex(index: Int) -> NSRect { return NSZeroRect }
}
2)改变了
的顺序$(document).ready(function() {
//do jQuery stuff when DOM is ready
});
最终结果是
<script type="text/javascript" src="Scripts/jQuery/book.js"></script>
<script type="text/javascript" src="Scripts/jQuery/jquery.min.js"></script>