我需要輸出所有這些div到一個新標籤。但是當我點擊一個鏈接時,只有第3行出貨。哪裏不對?Javascript輸出到一個新標籤
<a onclick="func()" href="javascript:void(0);">click to open all "foo" divs in a new tab</a>
<div class="foo">Row 1</div>
<div class="foo">Row 2</div>
<div class="foo">Row 3</div>
<script>
function func() {
var foo = document.getElementsByClassName("foo");
for (i = 0; i < foo.length; i++) {
htmlContent = foo[i].innerHTML;
}
window.open().document.write(htmlContent);
}
</script>
您正在用每次迭代替換'htmlContent' – Ramanlfc