jQuery是否总是返回数组? [英] Does jQuery always return array?

查看:77
本文介绍了jQuery是否总是返回数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

选择元素时,jQuery是否总是返回数组(当然,如果至少有一个元素存在)?示例:

Does jQuery always return array when selecting element (of course if at least one element exists)? Example:

$('#Myelement')
$('div')
$('tbody')

如果选择器是ID,该怎么办? 如果选择器是一个元素但只有一次出现该怎么办?

What if the selector is an ID? What if the selector is an element but has only one occurrence?

推荐答案

jQuery函数始终返回 jQuery 对象(基于数组),即使没有与选择器匹配的元素也是如此.

The jQuery function always returns a jQuery object (that is based on an array), even if there are no elements that matches the selector.

这样,即使没有匹配的元素,您也可以始终调用应该影响找到的元素的方法.如果jQuery对象不包含任何元素,则将什么都不做.

That way you can always call a method that is supposed to affect the elements found, even if there are no elements that matched. If the jQuery object contains no elements, it will simply do nothing.

如果您需要知道jQuery对象是否包含任何元素,可以使用length属性.

If you need to know if a jQuery object contains any elements, you can use the length property.

这篇关于jQuery是否总是返回数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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