javascript - HTML onclick 处理程序不触发 insideHTML 更改

标签 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 />

当我记录 playareainnerHTML 时,我得到

<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 与数据变量相同?

最佳答案

更改(数据内)

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

进入

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

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

关于javascript - HTML onclick 处理程序不触发 insideHTML 更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25061436/

相关文章:

php - Highcharts 和 Mysql

javascript - 在当前返回 Uncaught TypeError : Cannot read property 'weight' of undefined 的节点 d3.js 之间创建链接

javascript - 使用 Lodash 和 React 对数字字符串进行排序?

html - datebox 中的 dateFormat 不会生效

onclick - Aptana 显示警告 "input proprietary attribute onclick"

Javascript 从输入复选框转换数组

css - <nav> 中的左右对齐链接

javascript - 尝试通过 AJAX 从多个窗口传递多个变量

Javascript:Div onclick 不适用于某些 div 内容

javascript - 动态window.open方法的语法