javascript 选择长度 vs. options.length
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19301712/
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
Select length vs. options.length
提问by Christophe
For a select element, is there any difference between the length
property and the options.length
property?
对于select元素,length
property和options.length
property有区别吗?
In particular, I'd be interested to know if there's a difference in terms of browser support.
特别是,我很想知道在浏览器支持方面是否存在差异。
回答by Mike 'Pomax' Kamermans
Based on https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElementthere is no functional difference, only a "semantic if you want to get really technical about it" difference:
基于https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement没有功能差异,只有“如果你想真正了解它的技术性的语义”差异:
select.length
is formally declared as the number of option elements contained by a select element. It will by spec-definition always have the same value asselect.options.length
, which is:select.options.length
is "the number of elements in the list of options childNodes on the select element". Technical difference, semantically slightly different, but due to howselect.length
has been formalised, for all intents and purposes always the same value.
select.length
正式声明为 select 元素包含的 option 元素的数量。根据规范定义,它将始终具有与 相同的值select.options.length
,即:select.options.length
是“选择元素上的选项 childNodes 列表中的元素数”。技术差异,语义上略有不同,但由于select.length
形式化的方式,对于所有意图和目的始终具有相同的值。
So the first technically "lives" on the <select>
element, the second lives on the options
property of the <select>
element (which is an HTMLOptionsCollection, not an array!), but the value's always the same and it doesn't really matter which you use. Browsers that implement the spec (see [1] and [2]) always give the correct value for either.
所以第一个技术上“存在”在<select>
元素上,第二个存在于元素的options
属性上<select>
(它是一个 HTMLOptionsCollection,而不是一个数组!),但值总是相同的,你使用哪个并不重要。实现规范(见 [1] 和 [2])的浏览器总是给出正确的值。
[1] http://www.w3.org/TR/2002/PR-DOM-Level-2-HTML-20021108/html.html#ID-5933486
[1] http://www.w3.org/TR/2002/PR-DOM-Level-2-HTML-20021108/html.html#ID-5933486
[2] http://www.w3.org/TR/2002/PR-DOM-Level-2-HTML-20021108/html.html#HTMLOptionsCollectionwill
[2] http://www.w3.org/TR/2002/PR-DOM-Level-2-HTML-20021108/html.html#HTMLOptionsCollectionwill
回答by matewka
Both
两个都
select.length
选择长度
and
和
select.options.length
选择.options.length
are supported by all major browsers.
所有主流浏览器都支持。
The only difference between them (as far as I know) is
它们之间的唯一区别(据我所知)是
select.length
is select property which returns its number of options - that's the definition. In other wordslength
in select is a special property of this particular DOM elementselect.options.length
simply returns the number of elements inoptions
collection (the same logic asdocument.getElementsByTagName('div').length
)
select.length
是 select 属性,它返回它的选项数量——这就是定义。换句话说length
, select 是这个特定 DOM 元素的一个特殊属性select.options.length
简单地返回options
集合中元素的数量(与 相同的逻辑document.getElementsByTagName('div').length
)
回答by Martin Ritchie
It's not the same definitely I had to find out the hardway trying
这绝对不一样,我必须找出艰难的尝试
There are 3 options defined in the HTML/PHP
HTML/PHP 中定义了 3 个选项
Calling a JQUERY function and results are
调用一个 JQUERY 函数,结果是
Before selecting anything in the combobox
在组合框中选择任何内容之前
console.log(select.length); // 1
console.log(select.options.length); // undefined
After selecting something
选择东西后
console.log(select.length); // 3
console.log(select.options.length); // 3