2016-12-05 128 views
0

我試圖顯示如下所示的幻燈片: - (http://www.w3schools.com/w3css/w3css_slideshow.asp)位於頂部,當有人點擊「關於我們,但我甚至無法使onclick部分工作。」顯示div onclick無法正常工作

到目前爲止,我有: - ?

document.querySelector("p").addEventListener("click", function(){ 
 
    document.querySelector("div").style.display = "block"; 
 
});
#here{ 
 
    display: none; 
 
}

 
<div id="here">Slideshow</div> 
 
<p>About Us</p>

但它不工作的一些原因,有人能告訴我什麼是錯誤的點擊功能我試着給他們每個人的個人資料我不確定還有什麼可以嘗試的。預先感謝您提供的任何幫助。

+1

F12將彈出開發者控制檯。它會告訴你的錯誤.. – Pogrindis

+0

對我來說這是工作!然而,嘗試使用#here關於javascript 'document.querySelector(「#here」)。style.display =「block」;});' –

+0

好像頁面是**不完全加載,而腳本被執行 – Arvind

回答

3

它應該工作,你腳本位置是很重要的確保你的腳本您DOM後確定。或者您可以在DOMContentLoaded事件中添加腳本。

document.addEventListener("DOMContentLoaded", function(event) { 
    //script here 
}); 

document.querySelector("p").addEventListener("click", function(){ 
 
    document.querySelector("div").style.display = "block"; 
 
});
#here{display: none;}
<div id="here">Slideshow</div> 
 
<p>About Us</p>