使用 javascript 重新加载 div 的内容

发布于 2024-12-01 12:29:53 字数 367 浏览 0 评论 0原文

我试图在单击按钮时重新加载 div 标记的内容。我可以使用以下方法来做到这一点:

function reload_div()
{   
    document.getElementById('div_id').innerHTML = 'blah blah blah';
}

但问题是, div 标记包含数千行,当我尝试使用之前的函数完成 div 重新加载时,浏览器将被冻结。我正在 stackoverflow 上寻找解决方案,但我发现的所有内容都是使用 ajax 或 jquery,而且我以前没有使用过这些库。那么,我可以仅使用 javascript 和 html 重新加载 div 内容吗?谢谢。

I'm trying to reload the contents of div tag on button-click. I can do it with using:

function reload_div()
{   
    document.getElementById('div_id').innerHTML = 'blah blah blah';
}

but the problem is, that div tag contains thousands of lines and when I try to acomplish the div-reloading with previous function, the browser will be freezed. I was searching for a solution on stackoverflow but all what I found is with using ajax or jquery and I haven't used those library before. So, can I reload the div contents using javascript and html only? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

灵芸 2024-12-08 12:29:53

我不确定为什么您首先要使用 javascript 加载如此多的内容,但您始终可以将 javascript 放入外部文件中,以便浏览器可以缓存它。如果浏览器加载脚本导致页面冻结,这应该会有所帮助。

请随意发布更多代码,如果这不适合您,我会看看。

I'm not sure why you would want to load so much content with javascript in the first place, but you could always put the javascript in an external file so the browser could cache it. That should help if it's the browser loading the script that is causing the page to freeze up.

Feel free to post more code and I'll take a look if that doesn't do it for you.

垂暮老矣 2024-12-08 12:29:53

我认为你不能完全“刷新”内容,我从来没有听说过。 (但是,我不是专家)但是,我听说过用 javascript 切换内容,请尝试链接此:

这是您的按钮 & div 和内容:

<a href="divrefresh()">Refresh it!</a>
<div id="content"><p>Some Stuff!</p></div>

这是底层的 js,你有一个在上面的链接中使用的函数。

function divrefresh() {
     document.getElementById('content').innerHTML = "Some new stuff!"
}

ETA:哎呀,我现在看到了你帖子的其余部分。我不确定为什么这会冻结浏览器。尝试一下 jQuery,它真的很容易使用,这就是为什么每个人都喜欢它。如果您找到了 jQuery 解决方案,我会毫不犹豫地尝试一下!发布它,我可以帮助你。

如果您真的不想使用 jQuery 或 Ajax,那么我认为唯一的其他解决方案是将这些行放入自己的 HTML 文档中,有一个 iframe,然后刷新 iframe。

You can't exactly "refresh" the content I don't think, I've never heard of that. (however, I'm not an expert) However, I have heard of switching out the content with javascript, try something link this:

here's your button & div with content:

<a href="divrefresh()">Refresh it!</a>
<div id="content"><p>Some Stuff!</p></div>

Here's the js underlying, you've got a function which is used above in the link.

function divrefresh() {
     document.getElementById('content').innerHTML = "Some new stuff!"
}

ETA: Oops, I see the rest of your post now. I'm not sure why that would freeze the browser. Try jQuery, it's really easy to use, that's why everyone loves it. If you've found a jQuery solution, I wouldn't hesitate to try it out! Post it and I can help you.

If you REALLY don't want to use jQuery or Ajax, then I think the only other solution is to put the lines in their own HTML document, have an iframe, and refresh the iframe.

勿忘初心 2024-12-08 12:29:53

您可以尝试使用 removeChild,以各种方式方式(我不确定以下代码是否会冻结浏览器,但值得一试):

1)

function reload_div() {
    // Remove all child nodes from div first
    var div = document.getElementById('div_id');
    while (div.firstChild) {
        div.removeChild(div.firstChild);
    }

    document.getElementById('div_id').innerHTML = 'blah blah blah';
}

2)

function reload_div() {
    // Remove the div first
    var parent = document.getElementById('div_id').parentNode;
    parent.removeChild(document.getElementById('div_id'));

    // Recreate the div
    var div = document.createElement('div');
    div.setAttribute('id', 'div_id');

    // Append the div to the parent
    parent.appendChild(div);

    document.getElementById('div_id').innerHTML = 'blah blah blah';
}

You could try using removeChild, in a variety of ways (I'm not sure whether the following code would freeze the browser or not, but it's worth a try):

1)

function reload_div() {
    // Remove all child nodes from div first
    var div = document.getElementById('div_id');
    while (div.firstChild) {
        div.removeChild(div.firstChild);
    }

    document.getElementById('div_id').innerHTML = 'blah blah blah';
}

2)

function reload_div() {
    // Remove the div first
    var parent = document.getElementById('div_id').parentNode;
    parent.removeChild(document.getElementById('div_id'));

    // Recreate the div
    var div = document.createElement('div');
    div.setAttribute('id', 'div_id');

    // Append the div to the parent
    parent.appendChild(div);

    document.getElementById('div_id').innerHTML = 'blah blah blah';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文