HTML onclick处理程序不会触发innerHTML更改

时间:2014-07-31 14:26:11

标签: javascript html onclick innerhtml

我正在进行饥饿游戏模拟,这是我的代码:

console.log(data);
document.getElementById("playarea").innerHTML = data;
console.log(document.getElementById("playarea").innerHTML);

当我将变量data记录到控制台时,它会给我以下结果,

<img src='img/fight.png'><br />
You grabbed a loaf of bread out of the crate, the girl from 9 comes to you for a fight.
<br />
<button onclick=fight('You grabbed a loaf of bread out of the crate, the girl from 9 
comes to you for a fight.','','',4,1,2)>
Punch in the head
</button><br />
<button onclick=fight('You grabbed a loaf of bread out of the crate, the girl from 9 
comes to you for a fight.','','',4,1,2)>Punch in the chest</button><br />

当我记录innerHTML playarea时,我得到了

<img src="img/fight.png"><br>
You grabbed a loaf of bread out of the crate, the girl from 9 comes to you for a fight.
<br>
<button onclick="fight('You" grabbed="" a="" loaf="" of="" bread="" out="" the=""  
crate,="" girl="" from="" 9="" comes="" to="" you="" for="" 
fight.','','',4,1,2)="">
Punch in the head
</button><br>
<button onclick="fight('You" grabbed="" a="" loaf="" of="" bread="" out="" the="" 
crate,="" girl="" from="" 9="" comes="" to="" you="" for=""    
fight.','','',4,1,2)="">
Punch in the chest
</button><br>

onclick中,它将每个单词分开并将它们视为变量。

如何使innerHTML与数据变量相同?

1 个答案:

答案 0 :(得分:2)

更改(在数据内)

<button onclick=fight('You grabbed ...','','',4,1,2)>

<button onclick="fight('You grabbed ...','','',4,1,2)">

如果值只是一个单词,则只能省略引号。