简体   繁体   English

如何使用Javascript将HTML代码加载到表中?

[英]How to load HTML code into a table using Javascript?

I'm building a simple website which consists of a header (the title and a menu) and a table, in which the content is placed (because then I can make the layout easier). 我正在构建一个简单的网站,其中包含一个标题(标题和菜单)和一个表格,其中放置了内容(因为这样我可以使布局更容易)。 The problem is that each time I change the category (click a link in the menu), the whole site becomes white for a short period of time, while it's loading. 问题是每次我更改类别(单击菜单中的链接)时,整个站点在短时间内变为白色,同时加载。 I'd like it so this doesn't happen, so I only need to load the content of the table, not the whole page. 我喜欢它所以这不会发生,所以我只需要加载表的内容,而不是整个页面。 I guess this could easily be done using frames, but I was told that they will stop being supported soon, and that it's a bad practice using them. 我想这可以很容易地使用框架完成,但我被告知他们将很快停止支持,并且使用它们是一个不好的做法。 Is there a way to do this? 有没有办法做到这一点?

For the time being, the site is here, so you can see the effect I'm talking about: 目前,网站就在这里,所以你可以看到我正在谈论的效果: http://bljak.org:1235/index.html http://bljak.org:1235/index.html .

Also, it would be the best if site was just HTML and Javascript, because I'm not really familiar with PHP (and I'm coding on a netbook with 512Mb of RAM, so I doubt I could start a server there). 此外,如果网站只是HTML和Javascript,那将是最好的,因为我并不熟悉PHP(我在512Mb RAM的上网本上编码,所以我怀疑我可以在那里启动服务器)。

You can use innerHTML to rewrite the text between the tag and its close tag: 您可以使用innerHTML重写标记及其close标记之间的文本:

<script type="text/javascript">
function changeText(){
    document.getElementById('tbl').innerHTML = '<table><tr><td>New Text</td></tr></table>';
}
</script>
<div id="tbl"></div> 
<input type="button" onclick="javascript:changeText()" value="Change Text"/>

I will give way based on jQuery - this is the most simple and short way. 我将基于jQuery让位 - 这是最简单和最简短的方法。

First, download the latest jQuery library - can be found here . 首先,下载最新的jQuery库 - 可以在这里找到。

Suppose you name your local file "jquery.min.js" have such line in your existing HTML code to "include" the jQuery library: 假设您将本地文件命名为“jquery.min.js”,在现有HTML代码中包含这样的行以“包含”jQuery库:

<script type="text/javascript" src="jquery.min.js"></script>

Now all you need is this code in your page and you're done: 现在您需要的只是页面中的代码,您已完成:

<script type="text/javascript">
    $(document).ready(function() {
        $("a").bind("click", function() {
            var href = $(this).attr("href");

            //bust nasty browser AJAX cache:
            href += "?nnn=" + parseInt(Math.random() * 1000000);

            $("#Container").load(href);
            return false;
        });
    });
</script>

Replace the Container with the ID of the actual element in your page that should contain the "body" then clicking any link will cause the linked page to be loaded into the container without full page reload. 使用页面中应包含“正文”的实际元素的ID替换Container ,然后单击任何链接将导致链接页面加载到容器中而不进行完整页面重新加载。

The code is pretty much straightforward, feel free to ask if you want to understand any part of it though. 代码非常简单,可以随意询问您是否想要了解它的任何部分。

jQuery is less than 100K so with modern internet speed it really shouldn't be any problem - pure JavaScript is of course possible, but will cause lots of headache in keeping it cross browser. jQuery不到100K所以现代互联网速度真的不应该是任何问题 - 纯JavaScript当然是可能的,但是会让它在浏览器中遇到很多麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM