我有
<div id="arrayValues">6,10,4,19,7,11</div>
<div id="results">
<div id="firstnumber">6</div>
<div id="secondnumber">10</div>
<div id="thirdnumber">4</div>
<div id="fourthnumber">19</div>
<div id="fifthnumber">7</div>
<div id="sixtnumber">11</div>
</div>
<div id="resultScript"></div>
我需要将这些值从arrayValues放入resultScript div。
我尝试了很多东西,我不确定他们为什么不工作。我试过的一件事是
var val = document.getElementById("arrayValues");
var a = document.getElementById("resultScript");
a.value = "val";
我也尝试过,这个似乎更好看,但我似乎错过了一些东西。
var values = document.getElementById("arrayValues").value
var results = document.getElementById("resultScript").innerHTML = "values";
答案 0 :(得分:1)
HTML:
<div id="arrayValues">6,10,4,19,7,11</div>
<div id="results">
<div id="firstnumber">6</div>
<div id="secondnumber">10</div>
<div id="thirdnumber">4</div>
<div id="fourthnumber">19</div>
<div id="fifthnumber">7</div>
<div id="sixtnumber">11</div>
</div>
<div id="resultScript"></div>
JavaScript的:
// Get all the elements with the numbers and make an array out of it
var result = Array.from(document.getElementById('results').querySelectorAll('div'))
.map(function (elt) { return elt.innerHTML }) // Get the numbers
.join(',') // Useless if really using comma as separator, but you can change it later
document.getElementById('resultScript').innerHTML = result