简体   繁体   English

当页面位于 iframe 中时,不能 select 使用 javascript 的单选按钮

[英]Cannot select the radio button using javascript when the page is in an iframe

I have an iframe on the first page that contains a second page.我在包含第二页的第一页上有一个 iframe。 The iframe does not have an id so I'll have to assign one to it using javascript. iframe 没有 id,所以我必须使用 javascript 为其分配一个。 After I assign the id I try to use javascript to click the radio button that have the value 'female' in it.分配 ID 后,我尝试使用 javascript 单击其中包含值“女性”的单选按钮。 However when I do, I get an error in the console that says: "ERROR: Execution of script 'categorize faces' failed. Cannot read property 'click' of null"?但是,当我这样做时,我在控制台中收到一条错误消息:“错误:脚本'分类面孔'的执行失败。无法读取属性'click' of null”? What am I doing wrong我究竟做错了什么

var myframe = document.getElementsByTagName("iframe")[0];
myframe.id = "newID";
document.getElementById("newID").contentWindow.document.querySelector("input[value='female']").click();

First Page:第一页:

<iframe src="http://chatwithibot.com/test2.php"></iframe>

Second Page:第二页:

<form action="/testx.php" method = "POST">
<input type="radio" name="gender" value="male"> Male<br>
<input type="radio" name="gender" value="female"> Female<br>
<input type="radio" name="gender" value="other"> Other
<input type="submit" value="Submit">
</form> 

window.frames , .contentDocument , & .contentWindow.document window.frames.contentDocument.contentWindow.document

Iframe Exists when Page Loaded (Not Dynamic) Iframe 页面加载时存在(非动态)

See Demo 1见演示 1

Assuming that we are referencing the first (or only) iframe on the page:假设我们引用页面上的第一个(或唯一一个)iframe:

 window.frames[0] document.getElementsByTagName('iframe')[0].contentDocument; // interchangeable document.querySelector('iframe').contentWindow.document; // interchangeable

Store any one of the three lines from the example above into a variable, then treat that variable as document .将上面示例中的三行中的任何一行存储到一个变量中,然后将该变量视为document

 var iframeDoc = window.frames[0]; var iframeInput = iframeDoc.querySelector('#input2');

That in a nutshell is how to access a tag from parent page to a child page via iframe programmatically.简而言之,就是如何通过 iframe 以编程方式从父页面访问标签到子页面。 If a user's interaction is involved such the user clicking a radio button of the child page then the iframe's document should be registered to the click event using .addEventListener() or an Onevent Property .如果涉及用户的交互,例如用户单击子页面的单选按钮,则应使用 .addEventListener .addEventListener()Oneevent 属性将 iframe 的文档注册到单击事件。

 iframeDoc.addEventListener('click', eventHandler);

The Event Handler is a function called when iframeDoc is clicked.事件处理程序是单击 iframeDoc 时调用的iframeDoc

 function eventHandler(event) { console.log(event.target.value); }

eventHandler() passes the Event Object and then references the property Event.target . eventHandler()传递 事件 Object然后引用属性Event.target event.target is a direct reference to the clicked tag (ex. a radio button). event.target是对点击标签的直接引用(例如单选按钮)。 The .value property is suffixed to event.target to get the value of the clicked tag. .value属性后缀为event.target以获取点击标签的值。


Iframe is Dynamically Inserted Into DOM Iframe 被动态插入 DOM

See Demo 2见演示 2

When a tag is dynamically added to the DOM it cannot be registered to any event.当标签被动态添加到 DOM 时,它不能注册到任何事件。 A workaround is to use the Event Delegation pattern by registering an ancestor tag (ex. body) of the dynamic tag (ex. iframe).一种解决方法是通过注册动态标签(例如 iframe)的祖先标签(例如正文)来使用事件委托模式。 The eventHandler must "drill down" into the iframe document from an ancestor tag on the parent page. eventHandler 必须从父页面上的祖先标记“深入”到 iframe 文档。 See Demo 2 for the gruesome details.有关可怕的细节,请参见演示 2


Demo 1演示 1

Static Iframe Static Iframe

Note: due to security measures on SO, iframes are crippled.注意:由于 SO 的安全措施,iframe 被削弱。 The iframe in this demo is using srcdoc to roughly represent what the OP ( O riginal P ost) code has.此演示中的iframe使用srcdoc来粗略地表示OP(原始帖子)代码的内容。

 var xFrame = window.frames[0]; xFrame.onclick = extractValue; function extractValue(e) { console.log(e.target.value); }
 <iframe srcdoc='<form action="/testx.php" method="POST"><input type="radio" name="gender" value="male"> Male<br><input type="radio" name="gender" value="female"> Female<br><input type="radio" name="gender" value="other"> Other<br><br><input type="submit" value="Submit"></form>'></iframe>


Plunker普朗克

Demo 2演示 2

Dynamic Iframe动态 Iframe

Note: a working demo can be reviewed at this Plunker注意:可以在此Plunker上查看工作演示

 /* || This is to simulate the iframe being dynamically inserted into the DOM. || This is not required for OP (Original Post) specific circumstance. */ document.body.insertAdjacentHTML('afterbegin', `<iframe src='test2.html'></iframe>`); /* BEGINNING OF REQUIRED CODE */ /* || Register the body to the click event || function extractValue() is the event handler called when body is clicked. */ document.body.addEventListener('click', extractValue); /* Event Handler */ /* || Reference the Document Object inside iframe || Reference the form tag inside Document Object of iframe || Reference all radio buttons name='gender' in the form tag || Reference the selected radio button || Log the selected radio button's value */ function extractValue(e) { var xFrame = window.frames[0]; var xForm = xFrame.forms[0]; var xRads = xForm.elements['gender']; var selected = xRads.checked; console.log(selected.value); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM