gpt4 book ai didi

javascript - 将 div 中显示的文本复制到另一个 div

转载 作者:行者123 更新时间:2023-12-01 06:32:22 24 4
gpt4 key购买 nike

我在这里遇到一个问题,即将显示的文本从 div (显示来自 jquery)复制到另一个 div (使用 javascript)。

这是我的代码:

<body onload="copyDiv();">
<div id="first_div"></div>
<div id="second_div"></div>
</body>

<script>
// this function provides text for first_div
$(document).ready(function() {
$("#first_div").html('Testing');
});

// this function copies the text that comes from first_div to second_div
function copyDiv() {
var firstDivContent = document.getElementById('first_div');
var secondDivContent = document.getElementById('second_div');
secondDivContent.innerHTML = firstDivContent.innerHTML;
}
</script>

我的预期输出是,jquery 将提供第一个_div 的文本,而 javascript 函数将把文本从第一个_div 复制到第二个_div 中。提前致谢

最佳答案

这两个任务都应该按顺序加载:

$( document ).ready(function(){
$("#first_div").html('Testing');
$("#second_div").html($("#first_div").html());
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<body>
<div id="first_div"></div>
<div id="second_div"></div>
</body>

关于javascript - 将 div 中显示的文本复制到另一个 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60829351/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com