javascript noob...文档编写
我正在尝试使用 Javascript 在我的导航栏中加载更多链接。
这就是我尝试过的;我只想在导航中添加一个链接以在其下方加载更多内容。
<a href="" onclick="show()"/>collections</a>
<script type="text/javascript">
function show() {
document.write("collection 1 <br /> collection 2 <br /> etc... <br />");
}
</script>
有人可以建议相关教程或给我提示吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
document.write 实际上应该只在文档加载时使用。文档加载后调用它会清除当前文档并写入一些新内容。要向页面添加新内容,您需要创建新的 DOM 对象并将它们添加到页面或修改现有的 DOM 对象。
下面是一个修改页面的小示例,您可以在此处查看操作: http://jsfiddle.net/jfriend00/zVS39 /
HTML:
JavaScript:
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.
Here's a small example of modifying the page you can see in action here: http://jsfiddle.net/jfriend00/zVS39/
HTML:
Javascript:
您可以使用innerHTML,或者如果您想要更复杂的东西,您可以附加一个新元素,例如:
HTML
Javascript
You can use innerHTML, or if you want to something more complex you could append a new element, for example:
HTML
Javascript
请参阅 WSC 中的创建和修改 HTML
See Creating and modifying HTML from the WSC
你没有告诉我们你的问题是什么。
但这是错误的:
您已将
a
设置为自动关闭,因此“collections”不是链接的一部分,并且是孤立/无效的。因此,当您单击“集合”时,您的函数不会触发。
写:
You didn't tell us what your problem is.
But this is wrong:
You have made your
a
self-closing, so "collections" isn't part of the link and</a>
is orphaned/invalid. Thus your function isn't going to fire when you click on "collections".Write:
您始终可以使用innerHTML 功能。
所以你的 HTML 看起来像:
这样你想在导航下方插入的文本就会在“somelement”中弹出。您还可以修改 css 以执行显示/隐藏类型技术。
you can always use the innerHTML feature.
So your HTML would look like:
So that text you wanna insert underneath your navigation will pop up in "somelement". You can also modify the css to do a show/hide type technique.