简体   繁体   English

为什么使用jQuery的.append()函数和带有元素名称的jQuery对象以及作为单个参数的数组?

[英]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 , or offset . 从jQuery 1.4开始,可以传入任何事件类型,并且可以调用以下jQuery方法: valcsshtmltextdatawidthheightoffset

[…] [...]

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.

相关问题 追加jQuery不起作用 - append jquery does not work 更改元素的属性值后,jQuery函数不起作用 - Jquery function does not work after change element's attribute value 将元素放入数组时出现问题,使用分离和 append 到另一个使用 jquery 的元素单击不起作用 - Problem when put a element in array, using detach and append to another element using jquery click doesn't work 在 .append JQuery 中使用双引号和单引号将变量作为参数传递给 onclick 函数 - Pass variable as parameter to onclick function using double and single quotes in .append JQuery Jquery 函数对函数参数不起作用 - Jquery Functions does not work on function parameter 为什么使用innerHTML比JQuery的append()函数更慢地将大量元素附加到DOM? - Why is using innerHTML to append a large number of elements to the DOM slower than JQuery's append() function? 为什么使用数组作为键来访问对象中的元素? - Why does accessing an element in an object using an array as a key work? jQuery 函数作为其他 jQuery 函数的参数不起作用 - jQuery function as parameter of other jQuery function does not work jquery ready 函数如何允许它的第一个参数成为 jquery 对象? - How does jquery ready function allow it's first parameter to become jquery object? 为什么 jQuery element[] 选择器在这种情况下不起作用? - Why does the jQuery element[] selector not work in this case?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM