javascript
  • button
  • text
  • clear
  • 2016-12-03 89 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,有一個類似的問題被列)

    相關問題