Javascript 我的代码适用于jQuery 1.3.2,但不适用于1.6.4
我有以下代码可以在jQuery 1.3.2上完美运行:Javascript 我的代码适用于jQuery 1.3.2,但不适用于1.6.4,javascript,jquery,Javascript,Jquery,我有以下代码可以在jQuery 1.3.2上完美运行: $("#passwordLabel").click(function() { $(this).hide(); $("#password").focus(); } ); $("#confirmpasswordLabel").click(function() {
$("#passwordLabel").click(function()
{
$(this).hide();
$("#password").focus();
}
);
$("#confirmpasswordLabel").click(function()
{
$(this).hide();
$("#confirmpassword").focus();
}
);
$("#password").focus(function()
{
$("#passwordLabel").hide();
}
);
$("#confirmpassword").focus(function()
{
$("#confirmpasswordLabel").hide();
}
);
$("#password").blur(function()
{
if($(this).val()=="")
{
$("#passwordLabel").show();
}
}
);
$("#confirmpassword").blur(function(value)
{
if($(this).val()=="")
{
$("#confirmpasswordLabel").show();
}
}
);
});
但不幸的是,当我将jQuery库更改为1.6.4版时,这些代码不再有效
这是因为jQuery1.6.4不再有这种语法了吗?顺便说一句,你的名字不错 它看起来像是JavaScript的新版本。让我给你一些建议。首先,您应该尽量减少向浏览器请求元素的次数。使用jQuery,您所要做的就是将所选元素保存到变量中。我个人喜欢在我的元素变量前面加上$,这样我就知道jQuery元素不存在了
//too many selections
$('element').foo(); //query 1
$('element').bar(); //query 2
$('element').baz(); //query 3
//better
$element = $('element'); //query 1
$element.foo();
$element.bar();
$element.baz();
第二件事是,你应该始终保持你的{与块的开头在同一行。在大多数语言中,这并不重要,在很多情况下,它可以在JavaScript中工作,但由于分号插入,最好保持你的{在同一行
//one example of why this is bad
return
{
"foo": "bar"
};
//The above actually becomes
return;
{
"foo": "bar"
};
//should have actually been
return {
"foo": "bar"
};
我注意到的最后一件事是,当单击相应的标签时,javascript会关注每个输入。这实际上可以在没有javascript的情况下完成。您所需要做的就是将for属性添加到标签中。它们应该设置为相应输入的id
<!-- from this -->
<label id="passwordLabel">Password</label>
<input id="password" type="password">
<!-- to this -->
<label id="passwordLabel" for="password">Password</label>
<input id="password" type="password">
如果您想看到一些工作代码,我用上面的示例和一些示例输入为您制作了一个jsFiddle:非常感谢您回答这个问题 我相信我们以前的代码没有逻辑错误。 我应用了你的代码,它的行为仍然是一样的。。。 我认为jQuery版本1.3.2和1.6.4在使用这两个版本时存在问题 让我为这个问题添加一点可能是问题原因的信息: 我们正在构建一个需要使用jQuery进行验证的注册页面。在该页面上,我们还使用jQuery滑块 因此,页面结构如下所示: 请检查此图像:
当我们删除include jQuery 1.3.2时,滑块动画工作正常,但即使使用您的代码,密码标签也不会重新出现。您预期会发生什么,实际会发生什么,以及会收到什么错误消息(如果有)?您需要更具体一些,而不是它不起作用。什么不起作用?jquery 1.6和1.7中支持语法设置演示,它支持您正在使用的两个jquery版本。
//Wait for the page to load
$(function() {
var $passwordLabel, $confirmpasswordLabel, $password, $confirmpassword;
//Pre select the form elements
$passwordLabel = $("#passwordLabel");
$password = $("#password");
$confirmpasswordLabel = $("#confirmpasswordLabel");
$confirmpassword = $("#confirmpassword");
//bind events for the password input
$password.focus(function() {
//on focus hide the label
$passwordLabel.hide();
});
$password.blur(function() {
if(!$password.val()) {
//if the input is empty then restore the label on blur
$passwordLabel.show();
}
});
//bind events for the confirm password input
$confirmpassword.focus(function() {
//on focus hide the label
$confirmpasswordLabel.hide();
});
$confirmpassword.blur(function() {
if(!$confirmpassword.val()) {
//if the input is empty then restore the label on blur
$confirmpasswordLabel.show();
}
});
});