简体   繁体   English

jquery焦点模糊传递参数

[英]jquery focus blur pass parameter

I can't seem to access the variable defaultValue down in my .blur() function. 我似乎无法在我的.blur()函数中访问变量defaultValue I've tried various stuff but with no luck. 我尝试了各种各样的东西,但没有运气。 So far I only get an empty object. 到目前为止,我只得到一个空物体。 What's wrong? 怎么了?

jQuery(document).ready(function(){

    jQuery('#nameInput, #emailInput, #webInput').focus(function(){      
        var defaultValue = jQuery(this).val();
        jQuery(this).val("");
    })
    .blur(function(defaultValue){   
         if(jQuery(this).val() == ""){
             jQuery(this).val(defaultValue);
         }
    }); 

});

Looks like the question is about the passing data into the .blur or .focus event. 看起来问题是将数据传递到.blur或.focus事件。 per jQuery API - http://api.jquery.com/blur/ 每个jQuery API - http://api.jquery.com/blur/

blur( [eventData ], handler(eventObject) )

So if you want to pass data - you can send a parameter to event - which will appear as data in event object. 因此,如果您想传递数据 - 您可以将参数发送到事件 - 它将在事件对象中显示为数据。

see this fiddle for more info 看到这个小提琴更多信息

http://jsfiddle.net/dekajp/CgP2X/1/ http://jsfiddle.net/dekajp/CgP2X/1/

var p = {
    mydata:'my data'
};

/* p could be element or whatever */
$("#tb2").blur(p,function (e){
    alert('data :'+e.data.mydata);
});

Because your code is wrong :-) you define var inside function ( var defaultValue ) which is then immediately wiped out. 因为你的代码是错误的:-)你定义了var inside函数( var defaultValue ),然后立即将其删除。

There are two solutions: define your var as a global var before you bind blur event, or store it in the data of object liket his (which I recommend): 有两种解决方案:在绑定模糊事件之前将var定义为全局变量,或者将其存储在对象数据中(我建议):

$(document).ready(function(){
    $('#nameInput, #emailInput, #webInput').focus(function(){      
        $(this).val("").data('defaultValue',jQuery(this).val());
    }).blur(function(defaultValue){   
        if($(this).val() == ""){
            $(this).val($(this).data('defaultValue'));
        }
    }); 
});

It seems to me that you don't understand the basics of JavaScript. 在我看来,你不了解JavaScript的基础知识。

First of all variables in JS are localized to function's scope, so you can't declare variable with var in one function and access it in other function 首先,JS中的变量被本地化为函数的作用域,因此您不能在一个函数中使用var声明变量并在其他函数中访问它

Second, you can't pass anything to DOM-event handler, except event-object, this is defined by the DOM specification, sometimes you can use event data parameter to the blur jQuery method. 其次,你不能将任何东西传递给DOM事件处理程序,除了event-object,这是由DOM规范定义的,有时你可以使用event data参数来blur jQuery方法。

Try this: 试试这个:

jQuery(document).ready(function(){
    var defaultValue;
    jQuery("#nameInput, #emailInput, #webInput").focus(function(){      
        defaultValue = jQuery(this).val();
        jQuery(this).val("");
    })
    .blur(function(){   
        if(jQuery(this).val() == ""){
            jQuery(this).val(defaultValue);
        }
     }); 

 });

First of all, you need to distinguish blur method (function) and handler (function) which is the argument to the blur . 首先,您需要区分blur方法(函数)和handler (函数),它是blur的参数。 You was trying to pass the defaultValue exactly to handler, but that can't be done. 您试图将defaultValue完全传递给处理程序,但是无法完成。 Inside handler the defaultValue would be equal eventObject , so you can do smth like console.log(defaultValue.timeStamp) and you'll see smth like 123482359734536 在内部处理程序中, defaultValue将等于eventObject ,因此你可以像console.log(defaultValue.timeStamp)那样做smth,你会看到像123482359734536这样的123482359734536

In your approach you can't even use event.data argument to the blur cause it will be set at the time of blur 's call (attaching handler). 在你的方法中你甚至不能使用event.data参数blur因为它将在blur调用时设置(附加处理程序)。 You need to declare a var outside of the both handlers, so it will be visible to both of them 您需要在两个处理程序之外声明一个var ,因此它们都可以看到

You may consider to read some comprehensive book on JS. 您可以考虑阅读一些关于JS的综合性书籍。

I read "Professional JaveScript For Webdevelopers" by Nicolas Zakas. 我读了尼古拉斯扎卡斯的“专业JaveScript For Webdevelopers”。 There is a new edition 有一个新版本

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

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