简体   繁体   English

来自链接的JavaScript输入数据到iFrame中的文本字段

[英]Javascript input data from a link into text field within an iFrame

Good afternoon. 下午好。

<iframe width="550" id="iframe1" height="350" scrolling="no" src="blah.com/iframe.html"></iframe>

<script type="text/javascript">
    function insertTxt() {
        var textstring = "";
        parent.iframe1.document.getElementsByTagName("input")[0].value = textstring;
    }
</script>

<a href="#" onclick="insertTxt();">test 1</a>
<a href="#" onclick="insertTxt();">test 2</a>
<a href="#" onclick="insertTxt();">test 3</a>
<a href="#" onclick="insertTxt();">test 4</a>

The page which loads in the iframe has 1 input field but has no ID to latch onto. 在iframe中加载的页面具有1个输入字段,但没有ID可锁定。 I've been looking all over for a solution to get this to work. 我一直在寻找解决方案,以使其正常工作。 I'm trying to get the text 'test 1' or 'test 2' etc to get inserted into this input field within the iframe. 我正在尝试获取文本“ test 1”或“ test 2”等,以将其插入到iframe中的此输入字段中。

In the iframe file this is the input field html. 在iframe文件中,这是输入字段html。

<input type="text" autocomplete="off" spellcheck="true" maxlength="361" style="width: 100%; padding: 0px; border: 0px; margin: 0px; outline: 0px; background: rgb(255, 255, 255); color: rgb(0, 0, 0); font-family: Arial, Helvetica, sans-serif; font-size: 15px;">

If anyone is able to help me with the above mess. 如果有人能够帮助我解决上述问题。

Much appreciated. 非常感激。

First of all you are assigning blank string to variable textstring, and second if you are doing it without a webserver it will not work ( you'll get an error Uncaught DOMException , in console). 首先,您将空白字符串分配给变量textstring,其次,如果在没有网络服务器的情况下执行此操作将不起作用(在控制台中将收到错误Uncaught DOMException )。

Follow these steps to get it work 请按照以下步骤操作

  1. Use a webserver 使用网络服务器
  2. Add name="iframe1" on iframe 在iframe上添加name="iframe1"
  3. HTML - <a href="#" onclick="insertTxt(this);">test 1</a> <a href="#" onclick="insertTxt(this);">test 1</a>
  4. Change your function to 将功能更改为

     function insertTxt(el) { var textstring = el.text; parent.iframe1.document.getElementsByTagName("input")[0].value = textstring; } 

You may need to configure your server in order to do this, otherwise you'll get CORS errors. 为此,您可能需要配置服务器,否则会收到CORS错误。

I'm using querySelector here, but it should work with elementsByTagName and id as well. 我在这里使用querySelector,但它也应该与elementsByTagName和id一起使用。

iFrame.document is undefined, but you can get the contents of the iFrame like this. iFrame.document是未定义的,但是您可以像这样获取iFrame的内容。 If you don't get an error trying to do this, then you're good to go. 如果您在执行此操作时没有遇到任何错误,那么您就很好了。

var iFrame = document.querySelector('#frame1'),
    iFrameContent = iframe.contentDocument || iframe.contentWindow.document;

Then you should be able to query the input from there. 然后,您应该能够从那里查询输入。 If you know the document isn't going to change and is always going to look the same, you'll have to specifically get the input box you're looking for like this 如果您知道文档不会更改并且总是看起来一样,则必须专门获取要查找的输入框,如下所示

var n = <the n'th input>
var input = iFrame.querySelectorAll('input')[n];

With query selector you can get a little more specific and grab only the inputs where type is text like this: 使用查询选择器,您可以获得更具体的信息,并且仅获取类型为文本的输入,例如:

var m = <the m'th input where type is text>
var input = iFrame.querySelectorAll('input[type="text"')[m]

Then when you get your input, change the value of your input like so: 然后,当您获得输入时,请像这样更改输入的值:

input.value = "text here";

EDIT 编辑

An example of how to use this: 如何使用此示例:

function insertTxt(text) {
    /// get the iFrame and then it's 'document'
    var iFrame = document.querySelector('#frame1'),
        iFrameContent = iframe.contentDocument || iframe.contentWindow.document;
    // get the first input box with type text
    var input = iFrame.querySelectorAll('input[type="text"')[0] 
    // set the value of the input field to that text
    input.value = text; 
}

Changing the input field is easy. 更改输入字段很容易。 The issue you face now is the Cross domain Policy . 您现在面临的问题是“ 跨域策略” If that src of that iFrame isn't located on the same web domain you're trying to use this script on, then you'll get an error. 如果该iFrame的src不在您尝试使用此脚本的同一Web域中,那么您将收到错误消息。

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

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