在母版頁,我有以下代碼:的document.ready jQuery的難度
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>@ViewBag.Title</title>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("http://code.jquery.com/mobile/1.0rc2/jquery.mobile-1.0rc2.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.4.min.js")" type="text/javascript"></script>
<script src="@Url.Content("http://code.jquery.com/mobile/1.0rc2/jquery.mobile-1.0rc2.js")" type="text/javascript"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
@RenderBody()
</body>
</html>
然後在Index.cshtml我有以下代碼:
@{
ViewBag.Title = "Index";
}
<h2>
Index</h2>
<div data-role="page">
<div data-role="header">
...</div>
<div data-role="content">
<a id="btnShowCustomers" data-role="button" href="#secondDiv"></a>
</div>
<div data-role="footer">
...</div>
</div>
<div id="secondDiv" data-role="page">
<div data-role="content">
</div>
</div>
<script type="text/javascript">
(document).ready(function (event) {
$('#btnShowCustomers').bind('click', function (event) {
GetCustomers();
});
});
function GetCustomers() {
var webMethod = "Home/GetCustomers";
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: webMethod,
data: "{}",
dataType: "json",
success: function (dataObj) {
alert('lala');
}
});
}
</script>
調試使用Firebug我得到的以下錯誤:
document.ready不是函數 [Break On This Error](document).ready(function(event){
這怎麼可能?在文檔準備就緒後,我想註冊按鈕的單擊事件的處理程序。有什麼建議嗎?
將其替換爲'$(document).ready(...') - 您忘記在其中放置'$'變量。 – Lapple
對於jQuery包含,爲什麼你使用剃鬚刀nuget?因爲它是外部資源,所以應該替換''by腳本src =」http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.4.min.js「type =」text/javascript「>' –
你也可以使用'$(function(){'而不是'$(document).ready(function(){') – jgauffin