[英]How can I replace a jQuery object with an HTML element?
Here is my code: 这是我的代码:
$(".do").on("click", function(){ var stuffs_html = "", num = 2; $staffs_container = $(".staffs_container").clone().removeClass("hide_staffs_container"); for ( var i = 0; i < num; i++){ stuffs_html += $staffs_container; } $(".staffs_container").replaceWith(stuffs_html); })
.hide_staffs_container{ display: none; } input{ display: block; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="staffs_container hide_staffs_container"> <input class="staff_name" type="text" name="name" required placeholder="name" /> <input class="staff_mobile" type="text" name="mobile" required placeholder="mobile" /> <hr /> </div> <input type="button" class="do" value="click" />
As you can see, it shows [object]
instead of the HTML. 如您所见,它显示[object]
而不是HTML。 How can I fix the problem? 我该如何解决该问题?
Convert each item to html with this loc 使用此loc将每个项目转换为html
$staffs_container.html();
and also first empty the parent div, then append children to it 然后先清空父div,然后再将子代添加到父div
$(".staffs_container").removeClass("hide_staffs_container").empty().append(stuffs_html);
$(".do").on("click", function() { var stuffs_html = "", num = 2; $staffs_container = $(".staffs_container").clone(); for (var i = 0; i < num; i++) { stuffs_html += $staffs_container.html(); } console.log(stuffs_html) $(".staffs_container").removeClass("hide_staffs_container").empty().append(stuffs_html); })
.hide_staffs_container { display: none; } input { display: block; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="staffs_container hide_staffs_container"> <input class="staff_name" type="text" name="name" required placeholder="name" /> <input class="staff_mobile" type="text" name="mobile" required placeholder="mobile" /> <hr /> </div> <input type="button" class="do" value="click" />
$staffs_container[0]
contains the HTMLElement
, grab it then use .outerHTML
to get the HTML string $staffs_container[0]
包含HTMLElement
,抓住它然后使用.outerHTML
获取HTML字符串
$(".do").on("click", function(){ var stuffs_html = "", num = 2; $staffs_container = $(".staffs_container") .clone() .removeClass("hide_staffs_container"); for ( var i = 0; i < num; i++){ stuffs_html += $staffs_container[0].outerHTML; } $(".staffs_container").replaceWith(stuffs_html); })
.hide_staffs_container{ display: none; } input{ display: block; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="staffs_container hide_staffs_container"> <input class="staff_name" type="text" name="name" required placeholder="name" /> <input class="staff_mobile" type="text" name="mobile" required placeholder="mobile" /> <hr /> </div> <input type="button" class="do" value="click" />
I have updated you code..I think you expecting like this. 我已经更新了您的代码。。我想您期望像这样。
$(".do").on("click", function(){
var stuffs_html = "",
num = 2;
$staffs_container =
$(".staffs_container").clone().removeClass("hide_staffs_container");
for ( var i = 0; i < num; i++){
stuffs_html += $staffs_container.html();
}
$(".staffs_container").replaceWith($staffs_container);
});
http://jsfiddle.net/pf455/769/ http://jsfiddle.net/pf455/769/
Element objects and jQuery collections don't automatically output their markup when they're converted to strings (by +=
, in this case). 元素对象和jQuery集合在转换为字符串时(在本例中为+=
)不会自动输出其标记。 However, you don't necessarily need them to be strings. 但是,您不一定需要将它们作为字符串。
You can build up a collection (array) of element objects, which you can then provide to .replaceWith()
at once. 您可以建立元素对象的集合(数组),然后可以立即将其提供给.replaceWith()
。 You'll need to make more clones, though. 不过,您将需要创建更多克隆。
$(".do").on("click", function(){ var stuffs = [], num = 2; var $staffs_container = $(".staffs_container.hide_staffs_container"); for ( var i = 0; i < num; i++){ stuffs.push( $staffs_container.clone().removeClass("hide_staffs_container") ); } $(".staffs_container").replaceWith(stuffs); })
.hide_staffs_container{ display: none; } input{ display: block; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="staffs_container hide_staffs_container"> <input class="staff_name" type="text" name="name" required placeholder="name" /> <input class="staff_mobile" type="text" name="mobile" required placeholder="mobile" /> <hr /> </div> <input type="button" class="do" value="click" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.