jQuery多次追加对象 [英] jQuery Appending an Object multiple times
问题描述
我不确定这里到底发生了什么,我认为变量是一个jquery对象。
这仅追加一次,我的问题是为什么?
var newInput = $('< input />');
$(’#newDiv’)。append(newInput);
$(’#newDiv’)。append(newInput);
虽然这和我想的一样有效
var newInput ='< input>';
$(’#newDiv’)。append(newInput);
$(’#newDiv’)。append(newInput);
感谢您的帮助!
当您执行 $('< input />')
时,jQuery将创建一个输入
DOM元素。
当您 .append()
DOM元素时,它会分离从先前位置开始的元素。 (请参见小提琴)。来自文档:
如果以此方式选择的元素插入到DOM中其他位置的单个位置
中,它将被移至目标位置(未克隆)。
因此,您的第二个 .append()
调用将从其附加位置将其删除,并将其放置在新位置。 / p>
附加字符串时,将在附加DOM元素时创建它。
I am not sure what exactly is happening here, I think the fact that the variable is a jquery object.
This only appends once, my question is why?
var newInput = $('<input/>');
$('#newDiv').append(newInput);
$('#newDiv').append(newInput);
Though this works as I would assume
var newInput = '<input>';
$('#newDiv').append(newInput);
$('#newDiv').append(newInput);
Thank you for your help!
When you do $('<input/>')
, jQuery creates an input
DOM element for you.
When you .append()
a DOM element, it detaches the element from its previous position. (See Fiddle). From the docs:
If an element selected this way is inserted into a single location elsewhere in the DOM, it will be moved into the target (not cloned).
Thus, your second .append()
call will remove it from where it was appended first and place it in the new position.
When you append a string, the DOM element is created as it is appended.
这篇关于jQuery多次追加对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!