按下按钮时清除段落文本

问题描述 投票:1回答:2

我无法清除该按钮。因此,当我单击图像时,按钮和文本的确显示了,当我单击按钮时,按钮的确消失了,但文本没有。

<script type="text/javascript">
  function song(animal, sound) {
    document.getElementById("buttonAppear").innerHTML = '<button type="button" onclick="buttonFunction(); clear();">Clear</button>';
    document.getElementById("verse").innerHTML += "<br>Old MacDonald had a farm, E-I-E-I-O.<br>And on that farm he had a " + animal + ", E-I-E-I-O.<br>with a " + sound + "-" + sound + " here and a " + sound + "-" + sound + " there,<br>  here a " + sound + " there a " + sound + " everywhere a " + sound + "-" + sound + ".";
  }

  function clear() {
    document.getElementById("verse").innerHTML = "";
  }

  function buttonFunction() {
    document.getElementById("buttonAppear").innerHTML = "";
  }
</script>
</head>
<main>
  <h1>The Animals Game</h1>

  <img src="cat.jpg" title="Cat" alt="Cat" height="200" width="200" onclick="song('CAT', 'MEOW') ;">
  <img src="cow.jpg" title="Cow" alt="Cow" height="200" width="200" onclick="song('COW', 'MOO') ;">
  <img src="dog.jpg" title="Dog" alt="Dog" height="200" width="200" onclick="song('DOG', 'WOOF') ;">


  <p id="verse"></p>
  <div id="buttonAppear"></div>
javascript button text
2个回答
1
投票

事实证明,已经将'clear'作为全局引用,作为文档对象的属性。


1
投票

当调用onclick处理程序时,您不是在引用clear函数,而是引用document.clear函数(Is "clear" a reserved word in Javascript?)。尝试重命名函数

© www.soinside.com 2019 - 2024. All rights reserved.