简体   繁体   English

输入样式在IE中不起作用

[英]input style doesn't work in IE

I got a useful jQuery code, unfortunately its working great in Firefox but not in IE can anyone tell what was the problem and how to make the same action in IE too. 我得到了一个有用的jQuery代码,不幸的是,它在Firefox中运行良好,但在IE中却无法,任何人都无法分辨出问题所在以及如何在IE中执行相同的操作。

Thanks, Paul 谢谢保罗

My Code is given below 我的代码如下

HTML HTML

<input name="status" id="status" value="Type something here" type="text"/>

CSS CSS

#status{
        width:150;
        padding:5px;
        outline:none;
        height:20px;
    }
    .focusField{
        border:solid 2px #73A6FF;
        background:#EFF5FF;
        color:#000;
    }
    .idleField{
        background:#EEE;
        color: #6F6F6F;
        border: solid 2px #DFDFDF;
    }

JQuery JQuery的

<script type="text/javascript">
$(document).ready(function() {
    $('input[type="text"]').addClass("idleField");
    $('input[type="text"]').focus(function() {
        $(this).removeClass("idleField").addClass("focusField");
        if (this.value == this.defaultValue){
            this.value = '';
        }
        if(this.value != this.defaultValue){
            this.select();
        }
    });
    $('input[type="text"]').blur(function() {
        $(this).removeClass("focusField").addClass("idleField");
        if ($.trim(this.value == '')){
            this.value = (this.defaultValue ? this.defaultValue : '');
        }
    });
});
</script>

Try this: 尝试这个:

<script type="text/javascript">
    $(document).ready(function() {

        var $input = $("#status"),
            defaultVal = $input[0].defaultValue;

        $input.addClass("idleField");

        $input.focus(function() {

            $input.toggleClass("idleField focusField");
            if ($input.val() === defaultVal) { $input.val(""); } 
            else { $input.select(); }

        });

        $input.blur(function() {

            $input.toggleClass("focusField idleField");
            if ($.trim($input.val()) === "") { $input.val(defaultVal); }

        });

    });
</script>

i see you want to put the labels into the inut box. 我看到您想将标签放入输入框。 i think this is good, but there is a better and more "XHTML" solution: 我认为这很好,但是有一个更好,更多的“ XHTML”解决方案:

i think it would be better if you use default the tags and then if javascript enabled, hide them and copy into the input box. 我认为最好使用默认标签,然后启用JavaScript,将其隐藏并复制到输入框中。

my code: 我的代码:

(function($){
  /**
   *  This function moves the input <label> into the input field
   *  Creates a new input box with jquery clone, and changes the type to text, so the password fields label will be also visible. on focus and blr only the new input field will hided or shown, depended on the value of the original field.
   *  Example of usage:
   *  @code
   *    $("input#foo").inputLabel();
   *  @endcode
   *  
   *  @require jquery.js
   *  @param none
   *  @return none
   */
  $.fn.inputLabel = function() {
    var input = this
    var id;
    var label;
    var value;
    var isDefault;
    var fake;

    id = input.attr("id");                  //get the id of the input
    label = $("label[for="+id+"]").hide();  //search for the label and hide it
    value = label.html();                   //get the label text

    fake = input.clone();
    fake.attr("type","text");
    fake.attr("value",value);

    input.after(fake);
    input.hide();


    isDefault = true;

    var checkDefault = function() {
      if(input.attr("value")=='') {
        isDefault = true;
      }
      else {
        isDefault = false;
      }
    }

    fake.focusin(function() {
      if(isDefault) {
        fake.hide();
        input.show();
        input.focus();
      }
    });

    input.focusout(function() {
      if(isDefault) {
        input.hide();
        fake.show();
      }
    });

    input.keydown(function(event) {
        window.setTimeout(checkDefault,0);      
    });

    $(window).unload(function(){ //firefox bug fix.
      fake.attr("value","");
    });


    return this;
  }
})(jQuery);

this plugin is written by me. 这个插件是我写的。

if you want to use this, i think you should use this like that: 如果您想使用它,我想您应该这样使用:

<label for="register-username">username</label> <input id="register-username" type="text" class="register-ok" />
<label for="register-password">password</label> <input id="register-password" type="password" class="register" />

and the JS code to enable: 和启用以下代码的JS代码:

   $("#register-username,register-password").each($(this).inputLabel(););

after this you can easily add focus or other styles. 之后,您可以轻松添加焦点或其他样式。

In your jQuery, try replacing: 在您的jQuery中,尝试替换:

'input[type="text"]'

with: 有:

'#status'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM