javascript中的getElementByID[object HtmlPanelElement]
我有以下问题。。。我想将div的文本替换为span的其他文本 所以我试了一下:javascript中的getElementByID[object HtmlPanelElement],javascript,getelementbyid,Javascript,Getelementbyid,我有以下问题。。。我想将div的文本替换为span的其他文本 所以我试了一下: <main> <span id="export_text">This text should be exported</span><br> <div id="import_text">Here the new text should be after pressing the button</div><hr> <a
<main>
<span id="export_text">This text should be exported</span><br>
<div id="import_text">Here the new text should be after pressing the button</div><hr>
<a onclick="run()"><button>run</button></a>
</main>
<script type="text/javascript">
function run() {
document.getElementById('import_text').innerHTML = document.getElementById('export_text');
}
</script>
应导出此文本
按下按钮后,新文本应显示在此处
跑
函数运行(){
document.getElementById('import_text')。innerHTML=document.getElementById('export_text');
}
现在,如果我按下按钮,div中的文本将替换为[object HTMLSpanElement],而不是span中的文本。我做错了什么
谢谢大家! 只需在导出文本选择器的末尾添加
.innerHTML
:
function run() {
document.getElementById('import_text').innerHTML = document.getElementById('export_text').innerHTML;
}
我还强烈建议您在JS中移动您的活动:
document.getElementById("myBtn").addEventListener("click", run);
更新的HTML:
<main> <span id="export_text">This text should be exported</span>
<br>
<div id="import_text">Here the new text should be after pressing the button</div>
<hr> <a><button id="myBtn">run</button></a>
</main>
应导出此文本
按下按钮后,新文本应显示在此处
只需在导出文本选择器的末尾添加.innerHTML
:
function run() {
document.getElementById('import_text').innerHTML = document.getElementById('export_text').innerHTML;
}
我还强烈建议您在JS中移动您的活动:
document.getElementById("myBtn").addEventListener("click", run);
更新的HTML:
<main> <span id="export_text">This text should be exported</span>
<br>
<div id="import_text">Here the new text should be after pressing the button</div>
<hr> <a><button id="myBtn">run</button></a>
</main>
应导出此文本
按下按钮后,新文本应显示在此处
您忘记将innerHTML
放在export\u text
element的末尾更改为document.getElementById('export\u text')。innerHTML
,不要只分配该元素。我仍然得到相同的值。您忘记将innerHTML
放在export\u text
元素的末尾。更改为document.getElementById('export\u text')。innerHTML
,不要只分配该元素。我仍然得到相同的值