问题描述
我不确定这里到底发生了什么,我认为变量是一个jquery对象。
这仅追加一次,我的问题是为什么?
var newInput = $('< input />');
$(’#newDiv’)。append(newInput);
$(’#newDiv’)。append(newInput);
虽然这和我想的一样有效
var newInput ='< input>';
$(’#newDiv’)。append(newInput);
$(’#newDiv’)。append(newInput);
感谢您的帮助!
当您执行 $('< input />')
时,jQuery将创建一个输入
DOM元素。
当您 .append()
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:
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多次追加对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!