我有以下问题...我想用跨度的其他文本替换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],而不是跨度中的文本。我在这里做错了什么?
谢谢!
.innerHTML
在导出文本选择器的末尾添加Justt :
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>
JSFiddle:http : //jsfiddle.net/ghorg12110/n4cwem28/1/
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句