使用 Javascript 函数重新加载 HTML 元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10912925/
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
Reloading an HTML element with a Javascript function
提问by DextrousDave
I have a flash object embedded inside a DIV. Now I want to use a link('a' tag) above that div for the user to be able to reload that flash dialer, or more specifically the div tag it is contained in(I cannot change the flash file in any way).
我在 DIV 中嵌入了一个 Flash 对象。现在我想在该 div 上方使用一个链接('a' 标签),以便用户能够重新加载该 flash 拨号器,或者更具体地说是它包含的 div 标签(我无法以任何方式更改 flash 文件)。
I've tried using this JS function below, but it does not work(does not reload the div and contents when hitting the 'reload' link.
我已经尝试使用下面的这个 JS 函数,但它不起作用(点击“重新加载”链接时不会重新加载 div 和内容。
<div class="userProfile">
<script type="text/javascript">
function refreshDialer(){
document.getElementById("dialerDiv1").contentWindow.location.reload(true);
}
</script>
<a href="javascript: refreshDialer()">Reload</a>
<div id="dialerDiv1">
<embed type="application/x-shockwave-flash" wmode="opaque"
src="http://dummysite.com"
width="301"
height="401"
id="popUpSwf"
name="popUpSwf"
quality="high"
border="none"
allowscriptaccess="always"
pluginspage="http://www.adobe.com/go/getflashplayer"
scale="noscale"
menu="false"
flashvars="#" />
</div>
</div>
What am I doing wrong here?
我在这里做错了什么?
Thanks
谢谢
回答by Playmaker
回答by Praveen Kumar Purushothaman
You can, but the way you have used is wrong. When you want to reload something, you can just append a search query, so that it refreshes the source.
可以,但是你用的方法不对。当您想重新加载某些内容时,您只需附加一个搜索查询,以便它刷新源。
For Eg., when there is a frequently changing image (say captcha) and you wanna load it again, without refreshing the browser, you can do this way:
例如,当有一个频繁变化的图像(比如验证码)并且你想再次加载它而不刷新浏览器时,你可以这样做:
Initial Code:
初始代码:
<img src="captcha.png" alt="captcha" />
Refreshed Code:
更新后的代码:
<img src="captcha.png?1" alt="captcha" />
So, the same technique can be used in your page too. So the DIV
's ID is 'dialerDiv1'
right, for that, if you use jQuery, you can refresh this way:
因此,同样的技术也可以用于您的页面。所以DIV
的 ID 是'dialerDiv1'
正确的,为此,如果你使用 jQuery,你可以这样刷新:
function refreshDialer()
{
var d = new Date();
document.getElementByTagName('embed')[0].setAttribute('src', + document.getElementByTagName('embed')[0].getAttribute('src') + d.getMilliseconds());
}
Or if you are not using jQuery, you need to use the setAttribute()
function this way:
或者,如果您不使用 jQuery,则需要以setAttribute()
这种方式使用该函数:
function refreshDialer()
{
var d = new Date();
$('#dialerDiv1 embed').attr('src', $('#dialerDiv1 embed').attr('src') + d.getMilliseconds());
}
Simple. Hope this helps.
简单的。希望这可以帮助。