我有一个 JavaScript 函数,它不会在页面刷新时触发。当我从另一个页面导航到该页面时它可以工作,但如果刷新页面则失败。
该函数所做的就是将页面中一个元素的高度分配给另一个元素的高度(如果它们都存在)。
jQuery(document).ready(function ($) {
if ($(".Video_Pane iframe").length && $(".Image_Pane img").length) {
$(".Video_Pane iframe").height($(".Image_Pane img").height())
}
}
有什么想法吗?控制台没有显示任何与此相关的错误
最佳答案
img
的存在并不意味着它已被加载(因此您可以读取其高度)。 刷新通常会使图像的缓存版本失效,因此必须重新加载。
尝试使用 load
事件(在窗口对象上)而不是 ready
jQuery(window).load(function ($) {
if ($(".Video_Pane iframe").length && $(".Image_Pane img").length) {
$(".Video_Pane iframe").height($(".Image_Pane img").height())
}
})
关于javascript - 我的 javascript 函数在页面刷新时不会触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35866049/