Does an Uncaught TypeError in jQuery come from code conflict?

时光总嘲笑我的痴心妄想 提交于 2019-12-08 02:58:25

问题


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.


回答1:


Alter your script like this

<script src="jquery.js"></script>
<script type='text/javascript'>

jQuery.noConflict();
jQuery("input#login_email").click(function () {
    jQuery(".input_wrapper").addClass("blurred filled");
});  

jQuery("input#login_email").blur(function () {
    jQuery(".input_wrapper").removeClass("blurred");
});
</script>


来源:https://stackoverflow.com/questions/4625354/does-an-uncaught-typeerror-in-jquery-come-from-code-conflict

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!