【发布时间】:2020-08-01 18:54:10
【问题描述】:
我有多个具有相同类名的 div。如何定位每个 div 并将其内容复制到另一个 div,编辑内容并将其复制回其原始 div? 这是我的代码; HTML
<div id="div-editor">
<h1>Title of div to be edited goes here</h1>
<p>Paragraph of div to be edited goes here</p>
</div>
<div class="div-content" onclick="makeDivEditable()">
<h1>Div one title</h1>
<p>Copy the content of div ONE and make it editable in the div editor</p>
</div>
<div class="div-content" onclick="makeDivEditable()">
<h1>Div two title</h1>
<p>Copy the content of div TWO and make it editable in the div editor</p>
</div>
<div class="div-content" onclick="makeDivEditable()">
<h1>Div three</h1>
<p>Copy the content of div THREE and make it editable in the div editor</p>
</div>
<div class="div-content" onclick="makeDivEditable()">
<h1>Div four title</h1>
<p>Copy the content of div FOUR and make it editable in the div editor</p>
</div>
JavaScript
function makeDivEditable(){
var divContent = document.getElementsByClassName('div-content');
var divEditor = document.getElementById('div-editor');
divEditor.innerHTML = divContent[0].innerHTML;
divEditor.contentEditable = true;
}
此代码仅影响第一个 div(div one)我如何使其也影响其他 div?
我希望它的方式是,如果我单击任何 div,其内容应复制到 div-editor。一旦我完成编辑,它应该被复制回原来的 div。
【问题讨论】:
标签: javascript html arrays copy