问题描述
我正在尝试使用 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 ...文档写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!