Javascript innerDocument.getElementById(<;elment ID>;)。单击();不';在Firefox/chrome中不起作用,在IE中起作用
我正在使用Iframes,通过从父html文件自动控制iframe的元素。 加载test.html时,我试图使用javascript上的函数click()从test.html文件中单击iframe.html文件中定义的链接:Javascript innerDocument.getElementById(<;elment ID>;)。单击();不';在Firefox/chrome中不起作用,在IE中起作用,javascript,Javascript,我正在使用Iframes,通过从父html文件自动控制iframe的元素。 加载test.html时,我试图使用javascript上的函数click()从test.html文件中单击iframe.html文件中定义的链接: function handleMainOnload(){ alert("main frame loaded"); var innerFrame = document.getElementById("frame1"); alert("hi-1" +
function handleMainOnload(){
alert("main frame loaded");
var innerFrame = document.getElementById("frame1");
alert("hi-1" + innerFrame.ownerDocument.title);
var innerDocument = frame1.document;
alert("hi" + innerDocument.title);
innerDocument.getElementById("input1").value = "Dynamically Inserted Text";
innerDocument.getElementById("a1").click();
}
<body onload="handleMainOnload();">
<iframe id="frame1" src="iframe.html"/>
<a id="a1" href="http://www.google.co.in"> Google.. click</a>
Test.html文件:
function handleMainOnload(){
alert("main frame loaded");
var innerFrame = document.getElementById("frame1");
alert("hi-1" + innerFrame.ownerDocument.title);
var innerDocument = frame1.document;
alert("hi" + innerDocument.title);
innerDocument.getElementById("input1").value = "Dynamically Inserted Text";
innerDocument.getElementById("a1").click();
}
<body onload="handleMainOnload();">
<iframe id="frame1" src="iframe.html"/>
<a id="a1" href="http://www.google.co.in"> Google.. click</a>
函数handleMainOnload(){
警报(“主机架已加载”);
var innerFrame=document.getElementById(“frame1”);
警报(“hi-1”+内部框架。所有者文档。标题);
var innerDocument=frame1.document;
警报(“hi”+innerDocument.title);
innerDocument.getElementById(“input1”).value=“动态插入的文本”;
innerDocument.getElementById(“a1”)。单击();
}
iframe.html文件:
function handleMainOnload(){
alert("main frame loaded");
var innerFrame = document.getElementById("frame1");
alert("hi-1" + innerFrame.ownerDocument.title);
var innerDocument = frame1.document;
alert("hi" + innerDocument.title);
innerDocument.getElementById("input1").value = "Dynamically Inserted Text";
innerDocument.getElementById("a1").click();
}
<body onload="handleMainOnload();">
<iframe id="frame1" src="iframe.html"/>
<a id="a1" href="http://www.google.co.in"> Google.. click</a>
在IE中加载test.html时,会自动单击此achor id=a1,但在除IE之外的任何浏览器上都不起作用。请提供帮助。来自:
click()-在元素上执行单击,就像用户手动单击它一样。在大多数浏览器中,click()仅适用于非“提交”或“重置”的表单输入元素。它不能用于模拟单击链接或表单提交按钮
我已经证实这就是为什么它对你不起作用的原因。此外,我还必须更改以下行:
var innerDocument = frame1.document;
到
将使用jQuery中的一种方法进行更新
编辑:
下面是在jQuery中如何实现的。本质上是绑定一个click事件,然后调用元素上的click()。这模拟了该操作,因为在超链接上没有执行单击的标准方式
$(document).ready(function() {
$("#frame1").ready(function () { //The function below executes once the iframe has finished loading
alert("iframe loaded");
$('#frame1').contents().find('#a1').click(function() {
alert("Hello");
$('#frame1').attr('src', $('#frame1').contents().find('#a1').attr("href"));
$('#frame1').load();
});
$('#frame1').contents().find('#a1').click();
});
});
创建事件,初始化它并在节点上分派它 例如,见
只需记住对包含要分派的节点的文档调用createEvent。您可以在不使用jQuery和锚定标记上的click事件处理程序的情况下执行此操作。(与IE 9、FF 15、Chrome 23确认) 它不起作用的原因(对于JackWilson的编辑来说)是[在IE中至少]innerFrame.document==innerFrame.ownerDocument,因此您实际上并没有在iFrame的内容文档中寻找锚。改用innerFrame.contentDocument (我还没有确认JackWilson在IE以外的浏览器中使用window.frames[]的解决方案,但有些浏览器可能需要通过索引而不是id引用window.frames[],有人知道吗?) 主页:“aclicktest.html”:
点击测试
函数clickLocal(){document.getElementById(“alocal”).click();}
函数clickIframe(){document.getElementById(“frame1”).contentDocument.getElementById(“aiframe”).click();}
Iframe页面:“iframeaclicktest.html”:
单击测试IFRAME
有趣的是,你的旧代码在IE中工作。什么版本?您定义了doctype吗?这可能是怪癖模式的行为