[英]Why does using jQuery's .append() function with a jQuery object with an element name and an array in it as the single parameter work?
I have a question about how jQuery's .append function works in the following segment of code that dynamically adds values to a select box: 我有一个问题,关于jQuery的.append函数如何在以下代码段中工作,这些代码段动态地将值添加到选择框:
for (var i=currentYear; i >= minYear; i--){
$('#year').append($("<option/>", { value: i, text: i}));
}
I already know that this code does work, as I've used .append in this way before after finding similar code online during a google search. 我已经知道这段代码确实有用了,因为我在谷歌搜索过程中在网上找到类似的代码之前就已经以这种方式使用了.append。 However, the webpage I found the similar code on ( https://stackoverflow.com/a/3155663/3120918 ) did not explain why it works.
但是,我在网页上找到类似的代码( https://stackoverflow.com/a/3155663/3120918 )并没有解释为什么它有效。 I ready the official jQuery documentation page on the .append function ( http://api.jquery.com/append/ ), but it did not say anything about passing in a single jQuery object containing both a self-closed option element and an array (
$("<option/>", {value: key, text: value })
) as the single parameter to the append function. 我准备好.append函数( http://api.jquery.com/append/ )上的官方jQuery文档页面,但它没有说明传递一个包含自闭合选项元素和array(
$("<option/>", {value: key, text: value })
)作为append函数的单个参数。 I was hoping that someone could explain to me how and why this works. 我希望有人可以向我解释这是如何以及为什么有效。
jQuery object containing both a self-closed option element and an array (
$("<option/>", {value: key, text: value })
)包含自闭合选项元素和数组的jQuery对象(
$("<option/>", {value: key, text: value })
)
Nah, that is a special form of the jQuery constructor for "creating elements" : 不,这是“创建元素”的jQuery构造函数的一种特殊形式:
If a string is passed as the parameter to
$()
, jQuery examines the string to see if it looks like HTML [ and if it does ], jQuery attempts to create new DOM elements as described by the HTML.如果将一个字符串作为参数传递给
$()
,jQuery将检查该字符串以查看它是否看起来像HTML [ 如果它 ],jQuery会尝试创建HTML所描述的新DOM元素。[…]
[...]
The second argument to
jQuery()
can accept a plain object consisting of a superset of the properties that can be passed to the.attr()
method.jQuery()
的第二个参数可以接受一个普通对象,该对象由可以传递给.attr()
方法的属性的超集组成。Important : If the second argument is passed, the HTML string in the first argument must represent aa simple element with no attributes.
要点 :如果传递了第二个参数,则第一个参数中的HTML字符串必须表示一个没有属性的简单元素。 As of jQuery 1.4, any event type can be passed in, and the following jQuery methods can be called:
val
,css
,html
,text
,data
,width
,height
, oroffset
.从jQuery 1.4开始,可以传入任何事件类型,并且可以调用以下jQuery方法:
val
,css
,html
,text
,data
,width
,height
或offset
。[…]
[...]
So what does happen here is the construction of a new <option>
element with given value and text, which is wrapped in a jQuery collection. 那么这里发生的是构造一个具有给定值和文本的新
<option>
元素,它包含在jQuery集合中。 This is then passed to append
, which (as usual) appends all elements from the collection to its elements - in here, the new option to your #year
<select>
element. 然后
append
其传递给append
,它(像往常一样)将集合中的所有元素附加到其元素 - 这里是#year
<select>
元素的新选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.