获取一个空的 JQuery 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/897331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting an empty JQuery object
提问by Tom Hubbard
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.
此外,对于选择的某些值,会显示一条额外的消息。
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 的变量。执行处理程序后,我会检查 Previous 是否为 null 或大小是否为 0。
It would be nice to initialize Previous to an empty JQuery object so as not to have to do the extra check for null.
最好将 Previous 初始化为一个空的 JQuery 对象,这样就不必对 null 进行额外的检查。
Doing a $() returns an object with the size of 1.
执行 $() 返回大小为 1 的对象。
Is there a way to create an empty Jquery object?
有没有办法创建一个空的 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(); } }); }
In the worst case I could do something like this:
在最坏的情况下,我可以这样做:
var Previous = { size : function () { return 0; } };
but that seems like overkill.
但这似乎有点矫枉过正。
回答by Magnar
This creates an empty jQuery-object:
这将创建一个空的 jQuery 对象:
$([])
Update:In newer versions of jQuery (1.4+), you can use:
更新:在较新版本的 jQuery (1.4+) 中,您可以使用:
$()
回答by Tom Hubbard
$();
Returning an Empty Set
As of jQuery 1.4, calling the
jQuery()
method with no arguments returns an empty jQuery set (with a.length
property of 0). In previous versions of jQuery, this would return a set containing the document node.
返回一个空集
从 jQuery 1.4 开始,调用
jQuery()
不带参数的方法会返回一个空的 jQuery 集(.length
属性为 0)。在以前版本的 jQuery 中,这将返回一个包含文档节点的集合。
Source: api.jquery.com
回答by cletus
My advice is don't do it that way. There are a lot easier ways of doing this. Consider:
我的建议是不要那样做。有很多更简单的方法可以做到这一点。考虑:
<select id="select" name="select">
<option value="msg_1">Message 1</option>
<option value="msg_2">Message 1</option>
<option value="msg_3">Message 1</option>
</select>
<div class="msg_1 msg_3">
...
</div>
<div class="msg_1">
...
</div>
<div class="msg_2">
...
</div>
$(function() {
$("#select").change(function() {
var val = $(this).val();
$("div." + val").show();
$("div:not(." + val + ")").hide();
});
});
Mucheasier. Basically give classes to indicate what to show and hide and then there is no tracking required. An alternative is:
很多更加容易。基本上给出类来指示要显示和隐藏的内容,然后不需要跟踪。另一种选择是:
$(function() {
$("#select").change(function() {
var val = $(this).val();
$("div").each(function() {
if ($(this).hasClass(val)) {
$(this).show();
} else {
$(this).hide();
}
});
});
});
回答by Bane Neba
try this
尝试这个
var new = $([]);
I ain't a good programmer but it gives u empty object :))
我不是一个好的程序员,但它给了你空的对象:))