简体   繁体   English

使用javascript重新加载div的内容

[英]reload contents of div using javascript

I'm trying to reload the contents of div tag on button-click. 我试图在单击按钮时重新加载div标签的内容。 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. 但是问题是, div标签包含数千行,并且当我尝试使用以前的功能完成div-reloading时,浏览器将被冻结。 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. 我一直在寻找关于stackoverflow的解决方案,但发现的全部是使用ajax或jquery的,而我之前从未使用过这些库。 So, can I reload the div contents using javascript and html only? 因此,我可以仅使用javascript和html重新加载div内容吗? Thanks. 谢谢。

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. 我不确定为什么首先要使用javascript加载这么多内容,但是您始终可以将javascript放在外部文件中,以便浏览器可以对其进行缓存。 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. 请随意发布更多代码,如果这样做对您不利,我将看一看。

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: (但是,我不是专家)但是,我听说过用javascript切换内容,请尝试以下链接:

here's your button & div with content: 这是内容的按钮和div:

<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. 这是js的基础,您在链接中上面使用了一个函数。

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

ETA: Oops, I see the rest of your post now. ETA:糟糕,我现在看到您的其余帖子。 I'm not sure why that would freeze the browser. 我不确定为什么会冻结浏览器。 Try jQuery, it's really easy to use, that's why everyone loves it. 试试jQuery,它真的很容易使用,这就是每个人都喜欢它的原因。 If you've found a jQuery solution, I wouldn't hesitate to try it out! 如果您找到了jQuery解决方案,我将毫不犹豫地尝试一下! 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. 如果您真的不想使用jQuery或Ajax,那么我认为唯一的解决方案是将这些行放入自己的HTML文档中,使用iframe,然后刷新iframe。

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): 您可以通过多种方式尝试使用removeChild (我不确定以下代码是否会冻结浏览器,但值得尝试):

1) 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) 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';
}

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

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