只刷新一个< div>在只有普通的Javascript页面 [英] Refreshing only one <div> in page with just regular Javascript

查看:102
本文介绍了只刷新一个< div>在只有普通的Javascript页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我不从服务器获取任何信息,但我想每隔N秒重新加载/刷新一次div,我该怎么做?

javascript新手:我试过类似于

 < script type ='text / javascript'> 
函数refresh(){
setTimeout(window.location.reload(),10000);
}

< / script>

< div id ='target'onLoad ='refresh()'>
<?
var =从网上抓取一些快速变化的内容
print var
?>
< / div>
< div>
一些没有刷新的东西
< / div>

如果我没有从服务器获取新信息,我不清楚我需要AJAX。 .so现在我想知道如何使它在javascript中工作编辑:我不希望使用一个库这个基本操作,所以理想情况下,我wouldn不使用jquery,原型等
编辑二:不知道为什么人们说div没有改变...它的内容是动态的,它是从网上抓取(说刮)...每次它去抢东西在源头上已经改变的东西...一个例子可能是从twitter获取搜索结果,这些结果变化非常迅速...

div>

是的,您确实需要Ajax,因为根据定义,这就是Ajax。特别是如果你想抓取另一个 网站的内容。我知道你说过要使用普通的javascript,但检查一下,也许你会喜欢这个。看看这个非常棒的jQuery插件。
https://github.com/jamespadolsey/ jQuery插件/ tree / master / cross-domain-ajax /

非常简单易用 ANYWHERE (例如您的内容来自的另一个网站)获取内容。您只需使用相同的jQuery.load()方法或.ajax()方法,就像您在自己的服务器上一样,除非您可以从任何地方抓取内容!



只是将插件脚本添加到页面(在jQuery之后),然后使用.load()方法,如 here



所以在你的情况下,你可以这样做:

  $('div#target').load('http://somewhere.com #newContent'); 

这将从somewhere.com获得#newContent并将其放置在您网站的#target中。



您可以使用JavaScript的 $ b

  setInterval(function(){
$(' div#target').load('http://somewhere.com #newContent');
},5000); //每5000毫秒重复一次函数

毫秒(即5秒)。



您也可以从您自己的网站获取内容:

  $('div#target').load('http://yoursite.com/some-page #someContent'); 

这会将#someContent和其中的任何内容从 http://yoursite.com/some-page 转换为#target on http://yoursite.com/whatever-the-current-page-is



所有所有这些都是加载内容的超级简单方法。 jQuery的大小只有31kb(缩小),我相信这是值得的。当jQuery可以做你想做的事情并且有效的时候,没有必要重新发明轮子,除非你想学习和学习JavaScript。如果你只是想让你的网站工作(最终的结果是最重要的),那么就给我介绍一下超简单的方法吧。


If I am not grabbing any information from the server but I want to reload/refresh a div every N seconds how do I do this?

New to javascript: I've tried something like

 <script type = 'text/javascript'>
    function refresh(){
        setTimeout(window.location.reload(), 10000);
    }

    </script>

    <div id='target' onLoad='refresh()'>
<?    
var =grab some rapidly changing content from the web    
print var
  ?>  
    </div>
    <div>
    some stuff that doesn't get refreshed
    </div>

Its not clear to me that I need AJAX if im not getting the new info from the server...so for now i'd like to know how to make it work just in javascript

EDIT: I prefer not to use a library for this basic operation so ideally I wouldn't use jquery, prototype etc. EDIT II: Not sure why people are saying the div isnt changing...the content in it is dynamic it is grabbed (say scraped) from the web...and everytime it goes to grab stuff the stuff has changed at the source...an example could be grabbing search results from twitter which change very rapidly...

解决方案

Yes you do need Ajax, because by definition, that is what Ajax is. ESPECIALLY if you want to grab content from another website.

I know you said you want to use plain javascript, but check this out, maybe you'll like this. Take a look at this awesome jQuery plugin. https://github.com/jamespadolsey/jQuery-Plugins/tree/master/cross-domain-ajax/

It VERY SIMPLE TO USE it let's you perform Ajax sing jQuery with one VERY BIG DIFFERENCE: you can grab content from ANYWHERE (e.g. another website where your content comes from). You just use the same jQuery.load() method or .ajax() method just like you would on your own server, except you can grab content from anywhere!

Just add the plugin script to the page (after jQuery), then use the .load() method as described here.

So in your case, you can do something like this:

$('div#target').load('http://somewhere.com #newContent');

That will get #newContent from somewhere.com and place it inside #target on your site.

You could do something like this using javascript's setInterval:

setInterval( function() {
    $('div#target').load('http://somewhere.com #newContent');
}, 5000); //repeat function every 5000 milliseconds

That will repeat whatever is inside the function(){} every 5000 milliseconds (aka 5 seconds).

You can also get content from your own site:

$('div#target').load('http://yoursite.com/some-page #someContent');

That will put #someContent and whatever is inside of it from http://yoursite.com/some-page into #target on http://yoursite.com/whatever-the-current-page-is

All in all, this is a super simple way to load content. jQuery is only 31kb in size (minified), and I believe it's worth it. There's no need to reinvent the wheel when jQuery can do what you want, and efficiently at that, unless you are trying to learn javascript in and out. If you just want your site to work (the end result is what matters), then give the super simple method i just explained a try.

这篇关于只刷新一个&lt; div&gt;在只有普通的Javascript页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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