设置JavaScript变量等于另一个

时间:2016-09-07 19:48:26

标签: javascript

我的JSFiddle: https://jsfiddle.net/rdvxnjon/

我的HTML就像这样:

<ul>
<li id="thing1">Kittens</li>
<li id="thing2">Rainbows</li>
<li id="thing3">Sunshine</li>
</ul>
<button onClick="butane()">Run It</button>

<p>I like <span id="thingres1"></span> and <span id="thingres2"></span>, and love <span id="thingres3"></span>.
</p>

同时我的JavaScript就像这样:

function butane(){
var thing1 = document.getElementById('thing1').innerHTML;
var thing2 = document.getElementById('thing2').innerHTML;
var thing3 = document.getElementById('thing3').innerHTML;

var thingres1 = document.getElementById('thingres1').innerHTML;
var thingres2 = document.getElementById('thingres2').innerHTML;
var thingres3 = document.getElementById('thingres3').innerHTML;

thingres1 = thing1;
thingres2 = thing2;
thingres3 = thing3;
}

这里的逻辑是,我想知道如何将span的值设置为各自的li ID。

1 个答案:

答案 0 :(得分:1)

将它分配给变量只会覆盖变量中的值,而不会覆盖元素的内容。 您需要直接覆盖元素的innerHTML属性:

thingres1.innerHTML = thing1.innerHTML;