问问题
1291 次
3 回答
3
You might try this. Use the .clone()
method. Also you don't need the .html(i) when you're using clone(). Because clone returns a jQuery object you don't even need to wrap a $() around it.
var attached = $("<p>");
var sandbox = $("#sandbox");
for (var i = 0; i < 8; i++) {
attached.clone().appendTo(sandbox);
}
Also if you want to clone the event handlers you should use .clone(true). See http://api.jquery.com/clone/ for more info about clone()
于 2012-04-15T20:41:19.600 回答
2
The jQuery .clone()
method is precisely for making deep copies. From the documentation:
Description: Create a deep copy of the set of matched elements.
Example:
var attached = $("<p>");
var sandbox = $("#sandbox");
for (var i = 0; i < 8; i++) {
$(attached).clone().appendTo(sandbox);
}
于 2012-04-15T20:42:38.193 回答
1
var attached = $("<p>");
var sandbox = $("#sandbox");
for (var i = 0; i < 8; i++) {
// How come reseting attached works? attached = $("<p>");
attached.clone().html(i).appendTo(sandbox);
}
于 2012-04-15T20:41:14.203 回答