javascript
  • button
  • text
  • clear
  • 2016-12-03 79 views 1 likes 
    1

    我无法获取用于清除文本的按钮。所以当我点击图片时,按钮和文本会显示,当我点击按钮时,它会消失,但文本不会。按下按钮时清除段落文本

    <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> 
    
    +0

    呃,哪儿的按钮? –

    回答

    1

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

    此处了解详情:

    Is "clear" a reserved word in Javascript?

    你应该改变你的函数名称到别的东西,像clearParagraph()

    +0

    是的,我改名为擦除,它的作品:) – Niko

    0

    当onclick处理程序是叫你不引用您的clear功能,而是document.clear函数(Is "clear" a reserved word in Javascript?)。尝试重命名你的函数集点击事件处理程序,而不是使用onclick属性(你可以在这里阅读更多关于onclick="" vs event handler,有一个类似的问题被列)

    相关问题