問題描述
我正在嘗試使用 Javascript 在我的導航欄中加載更多鏈接.
I'm trying to use Javascript to load more links in my nav bar.
這是我嘗試過的;我只想在導航中添加一個鏈接,以便在其下方加載更多內容.
This is what I tried; I just wanted one link in my nav to load more beneath it.
<a href="" onclick="show()"/>collections</a>
<script type="text/javascript">
function show() {
document.write("collection 1 <br /> collection 2 <br /> etc... <br />");
}
</script>
誰能推薦一個相關的教程或給我一個提示?
Can anybody suggest a relevant tutorial or give me a hint?
推薦答案
document.write 應該只在文檔加載時使用.在文檔加載完成后調用它會清除當前文檔并寫入一些新的內容.要向頁面添加新內容,您需要創建新的 DOM 對象并將它們添加到頁面或修改現有的 DOM 對象.
document.write should really only be used while the document is loading. Calling it after the document is loaded will clear the current document and write some new content. To add new content to the page, you would need to create new DOM objects and add them to the page or modify existing DOM objects.
這里有一個修改頁面的小例子,你可以在這里看到:http://jsfiddle.net/jfriend00/zVS39/
Here's a small example of modifying the page you can see in action here: http://jsfiddle.net/jfriend00/zVS39/
HTML:
<a href="#" onclick="show()">collections</a>
<span id="moreText"></span>
Javascript:
Javascript:
function show() {
var o = document.getElementById("moreText");
o.innerHTML = "<br>collection 1 <br /> collection 2 <br /> etc... <br />";
}
這篇關于javascript noob ...文檔寫入的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!