我们有一个使用 javascript 的网站,用于客户端脚本的原型(prototype),我们现在开始使用 jQuery 并从原型(prototype)进行转换。我对 jQuery 知之甚少,但我有足够的知识来解决这个问题。
我遇到了一个问题,是否可以像全局加载 $(document).ready(function(){});
因为我们有很多 js 文件并且我希望能够使用 jquery('#button').click()
之类的东西。
我见过的唯一例子是在页面本身而不是js文件上。如果可能,请发布一个简短的示例代码,谢谢!
最佳答案
您可以使用$(document).ready()
从包含的 js 文件中,只需确保使用 put <script>
在 jquery include 之后包含,所以 $
功能可用。
编辑:
<html>
<head>
<script type="text/javascript" src="/path/to/jquery.js"></script>
<script type="text/javascript" src="/path/to/your/file.js"></script>
</head>
<body>
</body>
</html>
// file.js
$(document).ready(function() {
// do whatever you want when the document finishes loading...
});
关于页面外的 jQuery 就绪函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5585638/