使用 javascript 重新加载 div 的内容
我试图在单击按钮时重新加载 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定为什么您首先要使用 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.
我认为你不能完全“刷新”内容,我从来没有听说过。 (但是,我不是专家)但是,我听说过用 javascript 切换内容,请尝试链接此:
这是您的按钮 & div 和内容:
这是底层的 js,你有一个在上面的链接中使用的函数。
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:
Here's the js underlying, you've got a function which is used above in the link.
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.
您可以尝试使用
removeChild
,以各种方式方式(我不确定以下代码是否会冻结浏览器,但值得一试):1)
2)
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)
2)