IE 特定 - 当设置为“”时,闪烁光标在文本类型输入中消失。通过jquery
所以我有一个文本类型的输入字段,它有一个默认值(YYYY),当它获得焦点时,YYYY 消失:
//inside document.ready
....
$("#year").focus(function(){
if(yearVal === "YYYY") {
$("#year").val("");
}
});
jQuery.validator.addMethod("numericCheck", function(value, element) {
return this.optional(element) || /^[0-9]{4}$/.test(value);
}, "Please enter a valid year");
$("#theForm").validate({
//set the rules for the field names
rules: {
year: {
required: true,
minlength: 4,
numericCheck: true
}
},
messages: {
year: {
required: "Please enter the year",
minlength : "Please enter a valid 4 digit year",
numericCheck : "Please enter a valid year"
}
},
errorPlacement: function(error, element) {
if(element.siblings("div.errorMessage").length)
{
element.siblings("div.errorMessage").remove();
}
element.parent().append('<div class="errorMessage"></div>');
element.siblings("div.errorMessage").html(error.html());
element.parents("tr.inputRow").removeClass("goodInputRow");
element.parents("tr.inputRow").addClass("errorInputRow");
},
onkeyup: false,
onfocusout: false,
onclick: false,
submitHandler: function(form) {
$(form).find(":submit").attr("disabled", true);
form.submit(); }
});
....
//inside theForm
<input type="text" id="year" name="year" style="width: 40px;" maxlength="4" value="YYYY" />
文本消失,光标应该在字段中闪烁。 这在 FF 中效果很好,但在 IE8 中光标不会闪烁(并且消失了)。焦点仍然在该字段上,一旦用户键入内容,光标就会返回,但在此之前光标不存在。
我正在使用 jquery 1.4.4 和 IE8。我已经能够单独重新创建它(即页面上没有其他内容运行)
我尝试过 $("#year").focus() ——没有骰子。我可以专注于其他领域并让它出现在那里,但我真的不想为了解决这个问题而必须转移焦点。
问:IE8中清除YYYY后有没有办法让光标闪烁?
编辑: 感谢您的回复 - 我已进行修改以包含更多相关细节。
So I have a input field of type text that has a default value (YYYY) and when it gains focus the YYYY disappears:
//inside document.ready
....
$("#year").focus(function(){
if(yearVal === "YYYY") {
$("#year").val("");
}
});
jQuery.validator.addMethod("numericCheck", function(value, element) {
return this.optional(element) || /^[0-9]{4}$/.test(value);
}, "Please enter a valid year");
$("#theForm").validate({
//set the rules for the field names
rules: {
year: {
required: true,
minlength: 4,
numericCheck: true
}
},
messages: {
year: {
required: "Please enter the year",
minlength : "Please enter a valid 4 digit year",
numericCheck : "Please enter a valid year"
}
},
errorPlacement: function(error, element) {
if(element.siblings("div.errorMessage").length)
{
element.siblings("div.errorMessage").remove();
}
element.parent().append('<div class="errorMessage"></div>');
element.siblings("div.errorMessage").html(error.html());
element.parents("tr.inputRow").removeClass("goodInputRow");
element.parents("tr.inputRow").addClass("errorInputRow");
},
onkeyup: false,
onfocusout: false,
onclick: false,
submitHandler: function(form) {
$(form).find(":submit").attr("disabled", true);
form.submit(); }
});
....
//inside theForm
<input type="text" id="year" name="year" style="width: 40px;" maxlength="4" value="YYYY" />
The text is gone and the cursor should be blinking in the field.
This works great in FF, but in IE8 the cursor does not blink (and is gone). The focus remains on the field, and once the user types something the cursor comes back, but until that happens the cursor is not there.
I'm using jquery 1.4.4 and IE8. I've been able to recreate it in isolation (i.e. nothing else running on the page)
I have tried $("#year").focus() -- no dice. I can focus on some other field and have it appear there, but I really don't want to have to bounce the focus around to fix this.
Question: Is there a way to get the cursor blinking after clearing the YYYY in IE8?
EDIT:
Thanks for the responses - I've modified to include more of the pertinent details.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
这在 IE8 中对我来说效果很好:)
Try this:
This Worked fine for me in IE8 :)
我刚刚在 FF4.0 和 IE8、7 和 6 中检查了以下代码。对我来说效果很好...
JavaScript:
HTML:
你的示例太精简了。例如,yearVal 从哪里来?另外,就像jball提到的那样,您的条件逻辑不正确
编辑:(在Win2008 Server 64位下检查)
I just checked the following code in FF4.0 and IE8, 7 and 6. Works fine for me...
JavaScript:
HTML:
Your example is too stripped out. For instance, where is yearVal coming from? Also, like jball mentioned, your condition logic is incorrect
EDIT: (Checked under Win2008 Server 64bit)