javascript 如何触发页面上 div 单元的刷新(示例)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7715893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 01:02:50  来源:igfitidea点击:

How to trigger refresh of a div unit on a page (Example given)

javascriptpage-refresh

提问by Anand S

I am checking out this link : http://www.businessinsider.com/the-worlds-weirdest-etfs-that-you-can-trade-today-2011-10#market-vectors-gaming-etf-nyse-bjk-1

我正在查看此链接:http: //www.businessinsider.com/the-worlds-weirdest-etfs-that-you-can-trade-today-2011-10#market-vectors-gaming-etf-nyse-bjk -1

The page is a slideshow where the user can either press the Next/Previous button or use the right/left arrow keys to navigate through the slideshow. Everytime the user does a navigation, the ad unit on the right sidebar does a refresh. Any idea how this can be effected?

该页面是一个幻灯片,用户可以在其中按下下一个/上一个按钮或使用右/左箭头键来浏览幻灯片。每次用户进行导航时,右侧边栏上的广告单元都会刷新。知道如何做到这一点吗?

回答by kapa

The advertisement is displayed in an iframe(so it is not a simple div, in which case you would have to reload content using Ajax).

广告显示在 中iframe(因此它不是简单的div,在这种情况下您必须使用 Ajax 重新加载内容)。

In the event handler for the navigation buttons, you simply have to reload this iframe.

在导航按钮的事件处理程序中,您只需重新加载 this iframe

You can either do this by using location.reload():

您可以使用以下方法执行此操作location.reload()

document.getElementById('myiframe').reload(true);
/*"true" means the URL should not be reloaded from cache*/

or setting the srcto its current value:

或将 设置src为其当前值:

var myIframe=document.getElementById('myiframe');
myIframe.src=myIframe.src;