IE特定 - 当jquery设置为""时,闪烁光标在文本类型的输入中消失

Jus*_*tin 3 html jquery internet-explorer-8

所以我有一个类型文本的输入字段,它具有默认值(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" />
Run Code Online (Sandbox Code Playgroud)

文本消失了,光标应该在字段中闪烁.这在FF中很有用,但在IE8中,光标不闪烁(并且消失了).焦点仍然在该字段上,一旦用户键入某些内容,光标就会返回,但在此之前,光标就不存在了.

我正在使用jquery 1.4.4和IE8.我已经能够独立地重新创建它(即在页面上没有其他任何东西)

我试过$("#year").focus() - 没有骰子.我可以专注于其他领域,让它出现在那里,但我真的不想反复关注以解决这个问题.

问题:在IE8中清除YYYY后,有没有办法让光标闪烁?

编辑:感谢您的回复 - 我已修改为包含更多相关细节.

小智 5

试试这个:

$('#year').focus(function () {
    if ($(this).val() === "YYYY") {
        $(this).val("");
        $(this).select();
    }
});
Run Code Online (Sandbox Code Playgroud)

这在IE8中对我来说很好:)