如何使用JavaScript刷新页面?
当前回答
如果您正在使用jQuery并希望刷新,请尝试在javascript函数中添加jQuery:
我想在单击oh an h3时从页面中隐藏iframe,对我来说这是有效的,但我无法单击允许我查看iframe的项目,除非我手动刷新浏览器。。。不理想。
我尝试了以下方法:
var hide = () => {
$("#frame").hide();//jQuery
location.reload(true);//javascript
};
将简单的Jane javascript与jQuery混合使用应该会奏效。
// code where hide (where location.reload was used)function was integrated, below
iFrameInsert = () => {
var file = `Fe1FVoW0Nt4`;
$("#frame").html(`<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/${file}\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen></iframe><h3>Close Player</h3>`);
$("h3").enter code hereclick(hide);
}
// View Player
$("#id-to-be-clicked").click(iFrameInsert);
其他回答
这里所有的答案都很好。由于问题指定了如何使用jquery重新加载页面,我只是想为未来的读者添加更多内容。
jQuery是一个跨平台JavaScript库,旨在简化HTML的客户端脚本。~维基百科~
因此,您将了解jquery或jquery的基础是基于javascript的。因此,在简单的事情上,使用纯javascript要好得多。
但如果您需要jquery解决方案,这里有一个。
$(location).attr('href', '');
$(document).on("click", "#refresh_btn", function(event)
{
window.location.replace(window.location.href);
});
使用location.reload()。
例如,要在单击id为“something”的元素时重新加载:
$('#something').click(function() {
location.reload();
});
reload()函数接受一个可选参数,该参数可以设置为true,以强制从服务器而不是缓存重新加载。该参数默认为false,因此默认情况下页面可以从浏览器的缓存中重新加载。
use
location.reload();
or
window.location.reload();
您不需要jQuery中的任何东西,要使用纯JavaScript重新加载页面,只需在location属性上使用reload函数,如下所示:
window.location.reload();
默认情况下,这将使用浏览器缓存(如果存在)重新加载页面。。。
如果您想强制重新加载页面,只需传递一个true值来重新加载方法,如下所示。。。
window.location.reload(true);
此外,如果您已经在窗口范围内,您可以删除窗口并执行以下操作:
location.reload();