简体   繁体   English

Jquery:将变量传递给下一个链接函数,这是正确的方法吗?

[英]Jquery: passing variable to the next chained function, is this the correct way?

I want to know if this is correct. 我想知道这是否正确。

$('.myfilter').focus(function(){
    var length = $(this).val().length; 
    if (length == 0) {
        dosomething
    }
}).blur(function(length){
    if (length == 0) {
        dowhatever
            }
})

Above i've simplified my code, im just checking if length == 0 on focus and blur for my input. 上面我简化了我的代码,我只是检查焦点上的length == 0和输入的模糊。 notice how I declared length in focus, but not in blur, but i added the variable name inside .blur(function(length){ . Is this the better way to get length accessible in .blur without having to re-declare var length = $(this).val().length; in .blur? 注意我如何声明焦点的length ,但不是模糊,但是我在.blur(function(length){添加了变量名称.blur(function(length){ 。这是在.blur获取length的更好方法,而不必重新声明var length = $(this).val().length; in .blur?

as opposed to this: 与此相反:

$('.myfilter').focus(function(){
    var length = $(this).val().length; 
    if (length == 0) {
        dosomething
    }
})

$('.myfilter').blur(function(length){
    var length = $(this).val().length;
    if (length == 0) {
        dowhatever
            }
})

the first code block is the better way to do this? 第一个代码块是更好的方法吗?

How about not declaring any variables at all... :) 怎么没有声明任何变量... :)

$('.myfilter').focus(function() {
    if ( this.value.length === 0 ) {
        dosomething();
    }
}).blur(function(length) {
    if ( this.value.length === 0 ) {
        dowhatever();
    }
});

Also, this: 这个:

$('.myfilter').bind('focus blur', function(e) {
    if ( this.value.length === 0 ) {
        e.type === 'focus' ? dosomething() : dowhatever();
    }
});

Using variables 使用变量

Usually you want to use a variable if you need to use a certain value multiple times. 通常,如果需要多次使用某个值,则需要使用变量。 However, in this case, you need the this.value.length value only once (in the if-header). 但是,在这种情况下,只需要一次this.value.length值(在if-header中)。 Therefore, you can directly inject this value inside the if-header. 因此,您可以直接在if-header中注入此值。

$(this) vs this $(this)vs this

In order to understand when to use which, you need to understand the difference between a DOM element object and a jQuery object. 为了理解何时使用哪个,您需要了解DOM元素对象和jQuery对象之间的区别。 A jQuery object is a wrapper object which contains zero or more DOM element objects (like an array). jQuery对象是一个包装器对象,它包含零个或多个DOM元素对象(如数组)。 It is important to understand that you can only call jQuery methods (like .addClass() ) on jQuery objects, not DOM elements directly. 重要的是要了解您只能在jQuery对象上调用jQuery方法(如.addClass() ),而不能直接调用DOM元素。

Inside event handler functions, the this value refers to the DOM element at which the event fired. 在事件处理函数内部, this值引用事件触发的DOM元素。 Now, if you want to call jQuery methods on that DOM element, you need to wrap that DOM element inside a jQuery object - you do this by writing $(this) . 现在,如果要在该DOM元素上调用jQuery方法,则需要将该DOM元素包装在jQuery对象中 - 您可以通过编写$(this)

$(this).addClass('selected'); // works fine
this.addClass('selected'); // throws Error

What I would normally do, if you don't want to recapture length , is store length in a property on this and then read it in the blur event, like so: 我通常会做的,如果你不想再放length ,是店length在属性this ,然后在读它blur的事件,就像这样:

$('.myfilter').focus(function(){
    this.val_length = $(this).val().length; 
    if (this.val_length == 0) {
        dosomething
    }
}).blur(function(){
    if (this.val_length == 0) {
        dowhatever
    }
})

The first code block won't work - the passed parameter to .blur() will actually be the event that triggered the blur call. 第一个代码块不起作用 - 传递给.blur()参数实际上是触发模糊调用的事件。

Also note that there's no need to use jQuery to obtain the value, it's a direct property of this (for input elements): 另外请注意,有没有必要使用jQuery获得的价值,它的直接属性this (用于输入元素):

$('.myfilter').blur(function(ev) {
    if (this.value.length === 0) {
        // dowhatever
    }
});

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

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