在以下代碼中,我在選擇框上設置了更改處理程序,以根據選擇的值顯示和隱藏一些後續問題。獲取空的JQuery對象
此外,對於選擇的某些值,還會顯示一條額外的消息。
爲了檢查是否需要隱藏額外的消息,我保留一個名爲Previous的變量。在執行處理程序時,我檢查前面是否爲空或大小爲0.
初始化上一個空的JQuery對象以便不必執行額外的空值檢查會很好。
做一個$()返回的1
大小的物體有沒有一種方法創建一個空的jQuery對象?
//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(); } }); }
在最壞的情況下,我可以做這樣的事情:
var Previous = { size : function() { return 0; } };
,但似乎有點小題大做。
+1可愛的回答,我決不會已經想到這一點:) – 2010-05-27 12:32:51
我不明白這是怎麼從`$()不同`。任何人都可以投射燈光? – cregox 2011-03-02 00:40:15