2011-05-06 77 views
0

我得到一个错误,我假设它在我的语法。“F is undefined”使用直播活动

目的:如果用户实际上并没有写在任何text_area,然后填写回在其标签。

验证码:

$(".card_signup_form input").live('click', function(){ 
    $(this).css("color","#666666"); 
    $old_value = $(this).attr("value"); 
    $(this).attr("value", "") 
    if ($(this).live('keyup blur') && $(this).attr("value") == "") { 
    $(this).attr("value", $old_value); 
    }; 
}); 

jQuery的常识错误:

F is undefined 

回答

4

我觉得你的问题是在这里

if ($(this).live('keyup blur') ...) { 
    ... 
    }; 

现场正在寻找一个功能,又名“F”,绑定你没有提供。我建议将你的活结合分离成单独的代码块。

正如其他人已经指出的,只需绑定点击就不会为没有鼠标导航的用户工作。

2

这是我通常如何处理这个问题,你一定要在很多live事件 - 不确定是否不断向DOM添加新的文本字段。如果是这样,可能需要采取稍微不同的方法。

$('input').focus(function() 
{ 
    $(this).data('prev-val', $(this).val()) 
}) 
.blur(function() 
{ 
    if ($.trim($(this).val()).length == 0) 
    { 
     $(this).val($(this).data('prev-val')); 
    } 
}); 
+0

就像一个快速的旁注,“模糊”只适用于FF权利? – Trip 2011-05-06 20:22:17

+2

nono,模糊应该可以在所有浏览器中使用 – 2011-05-06 20:22:48

+2

'blur'是'onblur'事件,它已经存在很长时间了。如果你告诉我它不起作用,你会震惊和惊讶我。 – 2011-05-06 20:23:15

2

您可以通过指定模糊和焦点事件处理程序(焦点将处理点击)来做同样的事情。您还可以指定“默认”属性并将该值与默认值进行比较。我还会指定一个“默认”css类,而不是直接处理属性(只是我的前缀)。

$(".card_signup_form input").live("focus", function() 
{ 
    var $this = $(this); 
    if($this.val() != $this.attr("default")) 
    { 
     $this.val(""); 
     $this.removeClass("default"); 
    } 
}).live("blur", function() 
{ 
    var $this = $(this); 
    if($this.val() == "") 
    { 
     $this.val($this.attr("default")); 
     $this.addClass("default"); 
    } 
} 
<style> 
.default{ 
    color:#666666; 
} 
</style>