在第二次加载时更改 iframe 样式,在 Internet Explorer 9 中不起作用
Change iframe style on second load, doesn't work in Internet Explorer 9
我正在尝试在第二次加载时更改 iframe 的样式。我试过这个:
$(function() {
var loadCnt = 0;
$('#iframem').load(function() {
if (loadCnt >0) {
$("#idiv").width(453).height(349);
$("#iframem").css("left", "-656px");
$("#iframem").css("top", "-250px");
}
++loadCnt;
});
});
.HTML:
<div style="overflow: hidden; width: 377px; height: 230px; position: relative;" id="idiv">
<iframe id="iframem" src="http://www.example.org" style="border: 0pt none ; left: -1px; top: -8px; position: absolute; width: 1680px; height: 867px;" scrolling="no"></iframe></div>
它运行良好,但它不适用于Internet Explorer 9。正确的方法是什么?
我认为您的方法存在逻辑问题,并且不确定您如何使其工作。由于 JavaScript 仅在页面加载后运行,这意味着在您的代码中,您实际上永远不会loadCnt
大于 1 - 因为每次加载页面时,它都会再次将其设置为 0。
为了实现你所描述的,你需要采取不同的方法,有几种方法可以做到这一点:
-
使用服务器端语言并使用会话变量,其中设置了负载计数
-
如果您的页面通过 Ajax 加载并注入到 DOM,那么,例如,如果您的代码位于主页上的第一个"真实"加载页面
loadCnt
,然后在页面之间移动时您再次"加载"主页,实际上您没有加载整个JavaScript脚本,只需将HTML重新注入文档,然后您可以添加使用该loadCnt
的侦听器。 -
使用 JavaScript localStorage - 在第一次加载时,您将设置在本地存储对象上与 0 相反,您将能够在以后加载时检索:
if (localStorage.getItem("counter") === null) { // Check if the item exists; if not set it to 0 as it is the first run localStorage.setItem('counter', 0); } else { // Get the counter - here you can do your work for when the page is loaded after the first load localStorage.getItem('counter'); }
-
使用 Javascript cookie 执行与前面逻辑相同的操作,但在 cookie 中(因为 localStorage 可能存在兼容性问题)
应始终使用 onload
属性来设置任何函数。在 Internet Explorer 中,不能通过代码设置 onload
事件。有办法做到这一点,我在答案的选项 2 中提到过。
选项 1
.HTML
<div style="overflow: hidden; width: 377px; height: 230px; position: relative;" id="idiv">
<iframe id="iframem" src="http://www.example.com" style="border: 0pt none ; left: -1px; top: -8px; position: absolute; width: 1680px; height: 867px;" scrolling="no" onload="myIframeLoadFunction()">
</iframe>
</div>
JavaScript 代码
var loadCnt = 0;
function myIframeLoadFunction(){
if (loadCnt >0) {
$("#idiv").width(453).height(349);
$("#iframem").css("left", "-656px");
$("#iframem").css("top", "-250px");
}
++loadCnt;
}
选项 2
您需要在元素在 HTML 上呈现后专门绑定 onload 事件。
JavaScript 代码
(function (selector) {
var loadCnt = 0, frame = $(selector).get(0);
if (frame) {
frame.onload = function () {
if (loadCnt >0) {
$("#idiv").width(453).height(349);
$("#iframem").css("left", "-656px");
$("#iframem").css("top", "-250px");
}
++loadCnt;
};
}
})('#iframem');
- 视频HTML没有'无法在Internet Explorer 11上工作
- Internet Explorer缺少占位符支持,特别是密码字段
- Internet Explorer中的文本换行错误
- 为什么我在Internet Explorer上看不到html元素
- internet explorer 8兼容模式下的Youtube api错误
- 当浏览器控制台未打开时,为什么要求会导致Internet Explorer 9超时
- Internet Explorer 8. Events
- jQuery表单验证适用于Mozilla和Internet Explorer,但不适用于Chrome或Safari
- element.dataset in Internet Explorer
- Facebook FB.init”;访问被拒绝”;在Internet Explorer 11中
- JavaScript不适用于internet explorer和Firefox,但适用于谷歌
- 在Internet Explorer中,向所选内容添加选项不起作用
- 在Internet Explorer中从二进制文件打开PDF
- elementproblem internet explorer
- 我可以使用哪些方法在Internet Explorer中禁用Alt+*X*
- 如何在Internet Explorer中使用javascript设置cookie
- 只有当alert()在带有AJAX的internet explorer中使用时,Javascript才会更新UI
- sharepoint站点的internet explorer中出现Javascript错误
- Internet Explorer重新加载IFrame
- 如何在不使用Internet Explorer的情况下从未挂起的HTML文件执行程序