jQuery 中未捕获的类型错误是否来自代码冲突?
Tumblr 的登录页面给许多人带来了灵感,作为一名业余开发者,我一直想看看他们是如何制作自己的登录表格。我去掉了多余的代码并提出了一个简化的版本,如下所示:
<div id="login_form_container">
<form method="post" action="logged-in.html" id="login_form">
<div class="input_wrapper">
<label for="login_email">Email address</label>
<input type="text" name="email" id="login_email" value="">
</div>
<button type="submit"><span>Log In</span></button>
</form>
</div>
看到我在其他页面上使用 jQuery 1.3,我尝试使用以下内容来获得良好的效果。这并不完全是他们的方式,而是一个非常简化的版本:
<script>
$("input#login_email").click(function () {
$(".input_wrapper").addClass("blurred filled");
});
$("input#login_email").blur(function () {
$(".input_wrapper").removeClass("blurred");
});
</script>
当这是页面上唯一的代码时,它工作得很好,但当我将它与 jQuery 的其他位一起放在页面中时,它返回一个错误
Uncaught TypeError: Cannot call method 'click' of null
(anonymous function)
:无论是 Chrome、Firefox 还是 Safari,都是一样的,所以这显然只是我的代码。
有谁知道问题是什么?
- 是否可能是由于页面其他地方的 jQuery 引用冲突(都工作正常)?
- 这是更明显的事情吗?
- 是因为我没有命名函数吗?
我很困惑为什么代码可以单独工作,但与页面中的其他元素结合使用时却不起作用。这让我难住了。
Tumblr's login page is an inspiration for many, and as a hobbyist developer I was looking to see how they made their login form. I took away the superfluous code and came up with a simplified version, like so:
<div id="login_form_container">
<form method="post" action="logged-in.html" id="login_form">
<div class="input_wrapper">
<label for="login_email">Email address</label>
<input type="text" name="email" id="login_email" value="">
</div>
<button type="submit"><span>Log In</span></button>
</form>
</div>
Seeing as I use jQuery 1.3 on other pages, I tried using the following to get their nice effects. It's not quite how they have it, but a very simplified version:
<script>
$("input#login_email").click(function () {
$(".input_wrapper").addClass("blurred filled");
});
$("input#login_email").blur(function () {
$(".input_wrapper").removeClass("blurred");
});
</script>
This works nice and dandy when this is the only code on the page, but when I put it in a page with other bits of jQuery, it returns an error of:
Uncaught TypeError: Cannot call method 'click' of null
(anonymous function)
It's the same whether Chrome, Firefox or Safari, so it's clearly just my code.
Does anyone know what the problem is?
- Is it likely to be due to conflicting jQuery references elsewhere in the page (which all work fine)?
- Is it something more obvious?
- Is it because I haven't named the functions?
I'm puzzled as to why the code works in isolation but not when combined with other elements in a page. This has me stumped.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样改变你的脚本
Alter your script like this