javascript noob ...文件写 [英] javascript noob... document write

查看:98
本文介绍了javascript noob ...文件写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用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 ...文件写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆