使用 HTML5 Boilerplate/CodeIgniter 加载 jQuery 时出现问题
我正在开始并在我的笔记本电脑上设置了 xampp 服务器。我已经下载了 html5boilerplate-codeigniter 代码并将其放入 xampp 服务器上的适当目录中,并在 apache 中设置了一个虚拟目录以便于浏览器访问。
我想尝试一段简单的 jQuery 代码(我以前从未使用过 jQuery),因此我从 codeigniter 中获取了一个视图文件,并在该文件中放置了一些 jQuery 教程代码。我放置的代码是:
<a href="http://jquery.com/">jQuery</a>
<!-- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script> -->
<script>
$(document).ready(function(){
$("a").click(function(event){
alert("As you can see, the link no longer took you to jquery.com");
event.preventDefault();
});
});
</script>
我注释掉了对 jQuery 的调用,因为我的理解是,html5boilerplate 已经加载了 jQuery。但是,单击 jQuery 链接会将我直接带到 jquery 页面。当我取消对 jQuery 调用的注释时...我收到警报。这告诉我样板文件没有加载 jQuery。这准确吗?
有问题的页面的代码在这里: http://pastebin.com/VfVgbpFY
我知道第 146 行(src=//ajax) 并已将 http: 添加到 src 属性,但是,相同的结果...没有警报。
我做错了什么,jQuery 无法加载。它位于样板模板的末尾,以加快页面加载速度(根据文档)...但它是否根本加载还是我的做法都是错误的?
感谢您的帮助!值得赞赏。
I am starting out and have set up a xampp server on my laptop. I have downloaded the html5boilerplate-codeigniter code and placed it into an appropriate directory on my xampp server and have set up a virtual directory in apache for easy browser access.
I wanted to try a simple jQuery piece of code (I have never used jQuery before) so I took one of my view files from codeigniter and placed some jQuery tutorial code in the file. The code I placed is:
<a href="http://jquery.com/">jQuery</a>
<!-- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script> -->
<script>
$(document).ready(function(){
$("a").click(function(event){
alert("As you can see, the link no longer took you to jquery.com");
event.preventDefault();
});
});
</script>
I commented out the call to jQuery becuase my understanding is, html5boilerplate already loads jQuery. However, clicking on the jQuery link takes me directly to the jquery page. When I uncomment the call to jQuery... I receive the alert. This tells me that boilerplate isn't loading the jQuery. Is this accurate?
The code of the page in question is here: http://pastebin.com/VfVgbpFY
I am aware of line 146 (src=//ajax) and have added http: to the src attribute, however, same result... no alert.
What am I doing wrong that jQuery wont load. It's positioned at the end of the boilerplate template, to speed up page load (according to documentation)... but is it even loading at all or am I going about this all wrong?
Thanks for any help! It is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
jQuery 在页面底部加载,并且您的脚本在其上方的内容区域中加载。您使用
$(document).ready()
是对的,但$
还不存在,所以它仍然无法工作。您需要将代码放入外部文件并在调用加载 jQuery 之后加载它。jQuery is loaded at the bottom of the page, and your script is loading in the content area above it. You're right to have used
$(document).ready()
but$
doesn't even exist yet, so it's still not going to work. You need to put the code into an external file and load it after the call to load jQuery.