2017-04-10 44 views
-1

我無法設法將我的Javascript函數鏈接到span標記中的'onclick'html屬性。
我不斷收到錯誤onclick屬性找不到Javascript函數

"Uncaught ReferenceError: closeAndRefresh is not defined" "at HTMLSpanElement.onclick"

我的代碼如下所示:

<div class="error"> 
    <div id="errorMessage"> 
     <span onclick="closeAndRefresh();">&#10006;</span> 
     <p id="message"> 
      <?php echo $confirmationMessage ?> 
     </p> 
    </div> 
</div> 


<script> 
    function closeAndRefresh() { 
     <?php $visibility = 'hidden' ?> 
     window.location.reload(true); 
    } 
</script> 

我還會加我的代碼截圖!
enter image description here 感謝您的幫助。

+0

當然,低於標誌不是問題;) –

+0

您是否嘗試將內聯腳本放在網頁的HTML上方? – GavinBrelstaff

+0

嘗試在window onload事件中包裝你的函數,或者更好地使用事件監聽器而不是點擊 –

回答

0

Your script is after your HTML. Include it before html so that your function is defined

function closeAndRefresh(){ 
 
    
 
     window.location.reload(true); 
 
}
<span onclick="closeAndRefresh();">&#10006;</span>

0

確保正確引用功能,並且還嘗試把這個腳本的上方或下方,因爲它似乎是function無法識別。

0

嘗試將腳本function closeAndRefresh()寫入html頁面的頭標記。

也許這個html頁面在你寫入body的時候無法加載腳本。

+0

感謝floem進行編輯。 –