javascript - 使用 javascript 刷新 iframe 时遇到问题

标签 javascript jquery iframe

这曾经有效,但由于某种原因它不再有效。我使用 JavaScript 来更改 iframe 的源,然后刷新它。原因是我想在查看变量之前将变量发送到 iframe 中。

我添加了一些调试代码,它显示“调试 1”,但不显示“调试 2”。

可能是什么问题?

$("#upload_iframe").attr("src", "/editor/upload/?dir=" + dir);
// ---------------------------------
// THESE LINES DON'T WORK - STOPS AFTER FIRST DEBUG IS RUN...
// ---------------------------------
alert('debug 1');
$("#upload_iframe").contentWindow.location.reload(true);
alert('debug 2');
// ---------------------------------
$("#upload_file").dialog('open');

最佳答案

当你想要访问 dom 而不是 jquery 对象时,你应该添加 [0] :

$("#upload_iframe")     //is an array of matching dom objects
$("#upload_iframe")[0]  //is the first matching dom object

您还可以添加随机字符串以防止浏览器缓存:

$("#upload_iframe")[0].contentWindow.location.href = "/editor/upload/?dir=" + dir + "&rid=" + Math.random();

关于javascript - 使用 javascript 刷新 iframe 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1346872/

相关文章:

javascript - iOS (iPad) 上的 iframe 内容裁剪问题

javascript - 如何在 Navigator Onsen UI 中推送或替换页面?

javascript - Node.js EPIPE 错误

php - 移动首页(cursebird 或 foursquare)

php - 使用混合身份验证登录 <iframe> 期间出现问题

javascript - 使用 iFrame 关闭外部点击页面上的菜单

javascript - 计算两个给定日期之间的天数的函数有时会产生不准确的结果

javascript - 我应该在 Genexus 上的表单刷新/申请的用户控件中使用什么事件?

javascript - 为什么带有连字符 (-) 的 id 选择器不适用于 javascript

javascript - 使用JS获取Content并创建iFrame