我是Jquery的新手。我写了一个小代码并在Tomcat 6上运行。不知何故它无法正常工作。 请参阅下面的代码。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript" src="/includes/javascript/jquery/jquery.js"></script>
<script type="text/javascript" src="/includes/javascript/jquery/jquery.metadata.js"></script>
<script>
$("body").on("click", function(){
alert("test");
});
</script>
</head>
<body>
test
</body>
</html>
请告诉我这段代码有什么问题。
答案 0 :(得分:2)
你需要把它包装成一个就绪的处理程序,如下所示:
$(function(){
$("body").on("click", function(){
alert("test");
});
// All other jQuery functions would go inside this same wrapper
});
希望这有帮助! 更多信息:http://api.jquery.com/ready/