javascript [object HTMLSpanElement] 中的 getElementByID
getElementByID in javascript [object HTMLSpanElement]
我有以下问题...我想用跨度的其他文本替换 div 的文本。
所以我尝试了这个:
<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>
现在,如果我按下按钮,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>
我有以下问题...我想用跨度的其他文本替换 div 的文本。
所以我尝试了这个:
<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>
现在,如果我按下按钮,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>