获取一个空的JQuery对象 [英] Getting an empty JQuery object

查看:88
本文介绍了获取一个空的JQuery对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我在选择框上设置了一个更改处理程序,以根据选择的值显示和隐藏一些后续问题.

In the following code I set up a change handler on a select box to show and hide some follow up questions based on the value of the selection.

此外,对于选择的某些值,还会显示一条额外的消息.

Further, for some values of the selection there is an extra message that is displayed.

为了检查是否需要隐藏多余的消息,我保留了一个名为Previous的变量.执行处理程序后,我检查Previous是否为null或大小是否为0.

In order to check to see if I need to hide the extra message, I keep a variable called Previous. Upon execution of the handler I check to see if Previous is null or if the size is 0.

最好将Previous初始化为一个空的JQuery对象,这样就不必对null进行额外的检查.

It would be nice to initialize Previous to an empty JQuery object so as not to have to do the extra check for null.

执行$()将返回一个大小为1的对象.

Doing a $() returns an object with the size of 1.

有没有办法创建一个空的Jquery对象?

Is there a way to create an empty Jquery object?


//Init function.
$(function(){
//Hold the previously selected object for the account type selection.

var Previous = null;  //Here is where I would like to initialize.
                      //something like Previous = $();


$("SELECT[name='AccountType']").change(
    function () {
        //Hide Previous message if there was one.
        if(Previous == null || Previous.size() > 0){ 
            Previous.hide();
        }

        //Show the message if found and save it as previous.
        Previous = $("#"+this.value+"_Msg").show();

        //Get fisrt question
        var FirstQuestion = $(".FirstQuestion");
        if(this.value === ''){
            FirstQuestion.hide();
        }else{
            //Manually show FirstQuestion.
            FirstQuestion.show();
        }
    });
}

在最坏的情况下,我可以做这样的事情:

In the worst case I could do something like this:


    var Previous = { size : function () { return 0; } };

但那似乎太过分了.

推荐答案

这将创建一个空的jQuery对象:

This creates an empty jQuery-object:

$([])

更新: 在较新版本的jQuery(1.4+)中,您可以使用:

Update: In newer versions of jQuery (1.4+), you can use:

$()

这篇关于获取一个空的JQuery对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆