[英]$(document).ready in javascript
我是javascript和jquery的新手。 我想使用$(document).ready注册一些事件处理程序。 例如,这样的代码,我的问题是,为了使用$(document).ready,我需要在页面顶部引用哪些javascript库? 感谢有人可以为我提供一个简单易用的示例来学习$(document).ready。
<script>$(document).ready(function()
{
// function implementation internals
});
</script>
预先感谢乔治
您需要的只是jQuery库。
您可以下载该库并从您自己的服务器中包含它,也可以使用提供该库的许多CDN之一。 例如:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
// do something useful
});
</script>
综上所述,
将sje397提供的<script>
标签放在页面的<head>
部分中,该标签提供了您唯一需要的库... jQuery。
(或者: <script src="http://code.jquery.com/jquery-1.4.4.js" type="text/javascript"></script>
)
阅读http://docs.jquery.com/Tutorials:How_jQuery_Works
而且您应该很好。
<html>
<head>
</head>
<body>
<div id="someElement">Click Me</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
(function($) {
$(document).ready(function() {
$('#someElement').bind('click', function(event) {
// event.preventDefault(); // you might want to do this if your event handler has a default action associated with it (e.g. a link that gets clicked with an href)
// do stuff on your event (change click to whatever you need)
});
});
})(jQuery);
</script>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.