简体   繁体   English

重新加载div而不刷新所有页面

[英]Reload a div without refresh all page

I have a question. 我有个问题。 So, In my project I have a div : 因此,在我的项目中,我有一个div:

<div class="right-bloc-pub-1">
   <script src="http://www.adcash.com/ad/display.php?r=23" type="text/javascript">
</div>

No, I want to reload this script when a button is reload. 不,我想在重新加载按钮时重新加载此脚本。 I tried : 我试过了 :

location.reload();

But it's reload all the page. 但这会重新加载所有页面。 It's possible to reload onlye the right-bloc-pub-1 with his script? 有可能用他的脚本重新加载right-bloc-pub-1吗? Thx in advance 提前Thx

<script language="text/javascript">
   function load_js()
   {
      var head= document.getElementsByTagName('head')[0];
      var script= document.createElement('script');
      script.type= 'text/javascript';
      script.src= 'source_file.js';
      head.appendChild(script);
   }
   load_js();
</script>

The main point is inserting a new script tag -- you can remove the old one without consequence. 要点是插入一个新的脚本标记-您可以删除旧的脚本标记而不会产生任何后果。 You may need to add a timestamp to the query string if you have caching issues. 如果存在缓存问题,则可能需要在查询字符串中添加时间戳。

You can use .load() with url and class selector. 您可以将.load()与url和类选择器一起使用。

$( "#ID" ).load( "http://www.adcash.com/ad/display.php?r=23 .right-bloc-pub-1" );

Loading Page Fragments 加载页面片段

The .load() method, unlike $.get(), allows us to specify a portion of the remote document to be inserted. 与$ .get()不同,.load()方法允许我们指定要插入的远程文档的一部分。 This is achieved with a special syntax for the url parameter. 这可以通过使用url参数的特殊语法来实现。 If one or more space characters are included in the string, the portion of the string following the first space is assumed to be a jQuery selector that determines the content to be loaded. 如果字符串中包含一个或多个空格字符,则假定字符串中第一个空格之后的部分是确定要加载内容的jQuery选择器。

You can create a method to reload the script, like this: 您可以创建一种方法来重新加载脚本,如下所示:

function reload()
{
    $.getScript('http://www.adcash.com/ad/display.php?r=23');
    return true;
}

You can also make it refresh every x milliseconds, you do this like so: 您还可以使它每x毫秒刷新一次,如下所示:

var intervalID = setInterval(function(){ reload; }, 3000);//Every 3 seconds

//To stop refreshing
clearInterval(intervalID);

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

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