jQuery onfocus onblur问题

Jam*_*lor 1 jquery onfocus onblur

我在使我onFocusonBlur事件正常工作时遇到了一些麻烦

这就是我得到的

var var1

$("input").focus(function() {

  var1 = $(this).val()

if ( $(this).val()==var1) {
       $(this).val('').css({'color': "#000", 'font-style': 'normal', 'font-weight': 'bold'});
   }  
    $(this).css({'background-color':'#d7df23' });    
});

$("input").blur(function() {
   if ( $(this).attr("value")=="") {
       $(this).val(var1).css({'color': "#666", 'font-style': 'italic', 'font-weight': 'normal'});
   }   
    $(this).css({'background-color':'#EEEEEE' });      
});
Run Code Online (Sandbox Code Playgroud)

这是我的HTML

<input type="text" id="tbTitle" value="Title">

<input type="text" id="tbTitle1" value="Title1">

<input type="text" id="tbTitle2" value="Title2">
Run Code Online (Sandbox Code Playgroud)

如果您不更改文本框的值,则此方法有效.

基本上我想得到输入的原始值并将其存储在var中,然后如果该字段为空,则将原始值返回.

目前它正在将文本框中的任何内容的值放在焦点上

有没有办法存储原始值并只将其更改为onBlur

这是我的jsfiddle的链接

Fen*_*ton 5

这是我的解决方案......

http://jsfiddle.net/Sohnee/YTTD5/4/

或者在代码中......

var originalValues = new Array();

$("input").focus(function() {

    if (!originalValues[this.id]) {
      originalValues[this.id] = $(this).val()
    }

    if ( $(this).val()==originalValues[this.id]) {
           $(this).val('').css({'color': "#000", 'font-style': 'normal', 'font-weight': 'bold'});
       }  
        $(this).css({'background-color':'#d7df23' });    
   });

    $("input").blur(function() {
       if ( $(this).attr("value")=="") {
           $(this).val(originalValues[this.id]).css({'color': "#666", 'font-style': 'italic', 'font-weight': 'normal'});
       }   
        $(this).css({'background-color':'#EEEEEE' });      
   });
Run Code Online (Sandbox Code Playgroud)