gpt4 book ai didi

javascript - 无法让innerHTML 在某一特定位置工作。我不明白

转载 作者:行者123 更新时间:2023-12-03 01:17:02 25 4
gpt4 key购买 nike

我不明白为什么 AttackGoblin 函数不会写入我的 HTML(上面的函数写入完全相同的空间,没有问题)。我根本无法让它写任何东西。函数 escape 效果很好,我觉得它是完全相同的东西。

我已经呆了一周了,只是为了好玩而学习,但这让我发疯。另外,我知道我不应该在 HTML 中编写脚本,但我现在正在尝试一步一步地进行。有人看到我需要修复什么吗?非常感谢。

   <!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<title>Status</title>
<link rel="stylesheet" type="text/css" href="reset.css" />
<link rel="stylesheet" type="text/css" href="style.css" />
<link href="https://fonts.googleapis.com/css?family=Oswald" rel="stylesheet">
</head>


<body>
<header>
<h1>Stupid Game</h1>
</header>

<div class="container">
<table>
<col width="90">
<col width="90">
<col width="600">
<thead>
<th colspan='2'>
<h1>Status</h1>

</th>
<th colspan="2">
<h1>Action

</h1>
</th>
<tr>
<th>
<h2>HP</h2>
</th>
<th>
<h2>
<script>
document.write(you._hp)

</script>
</h2>
</th>
<td id="actionTitle"> Click the door to enter the dungeon.</td>
</tr>
</thead>
<tbody>
<tr>
<td class="left">STR</td>
<td class="left">
<h3>
<script>
document.write(you._str)

</script>
</h3>
</td>
<td id="action" rowspan="5" colspan="4">
<div id="actionLeft" onclick="goblinFight()"><img src="https://images-na.ssl-images-amazon.com/images/I/713sQo9DrQL.png" height="200" width="200">

</div>
<div id="actionMiddle"> </div>
<div id="actionRight"> </div>

<p id="combatMessage">Good Luck!</p>
</td>



</tr>
<tr>
<td class="left">INT</td>
<td>
<h3>
<script>
document.write(you._int)

</script>
</h3>
</td>
</tr>
<tr>
<td class="left">DMG</td>
<td>
<h3>
<script>
document.write(you._dmg)

</script>
</h3>
</td>
</tr>
<tr>
<td class="left">Status Effects</td>
<td>
<h3> - </h3>
</td>
</tr>
<tr>
<td class="left">Gold</td>
<td>
<h3>
<script>
document.write(you._gold)

</script>
</h3>
</td>
</tr>

</tbody>
</table>
</div>

<footer>

</footer>

<script>

function goblinFight() {
document.getElementById("actionTitle").innerHTML = 'A goblin! Click the sword to attack or the door to flee.';
document.getElementById("actionLeft").innerHTML = '<div onclick="attackGoblin()"><img src = "http://iconbug.com/data/80/256/5f0a7369c9651132688f02cbc49a7c28.png" width="120" height="120"></div>';
document.getElementById("actionMiddle").innerHTML = '<img src = "https://marketplace.canva.com/MACg_sB88WY/1/thumbnail_large/canva-young-goblin-illustration-icon-MACg_sB88WY.png" width="240" height="240">';
document.getElementById("actionRight").innerHTML = '<div onclick="flee()"><img src = "http://piskel-resizer.appspot.com/resize?size=200&url=http%3A%2F%2Fwww.piskelapp.com%2Fimg%2F551041a6-7701-11e6-8d38-1bbce077a660.png" width="120" height="120"></div>';
document.getElementById("combatMessage").innerHTML = ''
}

function flee() {
document.getElementById("actionTitle").innerHTML = 'Record what you left with.';
document.getElementById("actionLeft").innerHTML = '<img src = "https://media.giphy.com/media/deWrNpLBRC60E/giphy.gif" width="300" height="300">';
document.getElementById("actionMiddle").innerHTML = '';
document.getElementById("actionRight").innerHTML = '';
document.getElementById("combatMessage").innerHTML = 'Write down anything you left with.';
}

function attackGoblin() {
document.getElementById("combatMessage").innerHTML = 'got him';
}
var you = {
_maxHp: 12,
_hp: 12,
_str: 10,
_int: 10,
_dmg: '5-8',
_gold: 0,
_attack: function() {
return Math.floor(Math.random() * 5 + 4)
}
};
var goblin = {
_hp: 8,
_gold: Math.random(Math.floor() * 4),
_attack: function() {
return Math.floor(Math.random() * 3 + 2);
}
}

最佳答案

第一次运行 goblinFight() 函数后,您最终会得到以下 HTML 结构:

<div id="actionLeft" onclick="goblinFight()">
<div onclick="attackGoblin()">
<img src="http://iconbug.com/data/80/256/5f0a7369c9651132688f02cbc49a7c28.png" width="120" height="120">
</div>
</div>

接下来,当您单击内部 div 时,您会触发 attackGoblin() 函数,并且它起作用了,但在此之后单击事件立即冒泡 到外部 div 并再次触发 goblinFight() 函数,这会重置第一个函数的效果。

为了防止这种情况发生,您应该将对事件对象的引用传递给 attackGoblin 函数(通过将 onclick 处理程序更改为 onclick="attackGoblin(event)")并使该函数阻止事件传播:

function attackGoblin(e) {
document.getElementById("combatMessage").innerHTML = 'got him';
e.stopPropagation(); // cancels the bubbling of the event passed into the function
}

希望这有帮助!

关于javascript - 无法让innerHTML 在某一特定位置工作。我不明白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51959580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com