我开始使用jQuery了.我一直试图将它与我之前存在的一些JavaScript代码混合在一起,所以我不必重写所有内容.我读过许多地方,这是完全可行的.但是,每当我包含任何jQuery行时,标准JavaScript就会停止运行.
以下是一些例子:
<!DOCTYPE html>
<html>
<head>
<style>
#testjquery {
height: 300px;
width: 300px;
}
</style>
</head>
<body>
<div id="testjquery" onclick="testClick()">This is the stuff.</div>
<script src='jquery-1.10.2.min.js'></script>
<script>
$(document).ready(function() {
function testClick() {
document.getElementById("testjquery").style.background = "blue";
}
});
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
这不起作用,当点击我得到一个未定义的函数错误.
但是,把它放在体内
<body>
<div id="testjquery">This is the stuff.</div>
<script src='jquery-1.10.2.min.js'></script>
<script>
$(document).ready(function() {
$("#testjquery").click(function() {
$(this).css("background", "blue");
});
});
</script>
</body>
Run Code Online (Sandbox Code Playgroud)
工作正常,与标准JavaScript中的等效工具一样.