Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/414.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript将文本放在iFrame的textarea中_Javascript_Jquery_Html - Fatal编程技术网

JavaScript将文本放在iFrame的textarea中

JavaScript将文本放在iFrame的textarea中,javascript,jquery,html,Javascript,Jquery,Html,是否可以使用JavaScript将文本放入iFrame中的文本区域 index.html <iframe src="http://othersite.com:3000"> 在iFrame中有一个文本区域 <textarea class="inp" placeholder="Send message..."></textarea> 有可能在其中添加文本吗?您不能修改iFrame的HTML,解决方案是:必须使用。如果不能修改“”的代码,则不可能 如果可

是否可以使用JavaScript将文本放入iFrame中的文本区域

index.html

<iframe src="http://othersite.com:3000">

在iFrame中有一个文本区域

<textarea class="inp" placeholder="Send message..."></textarea>


有可能在其中添加文本吗?

您不能修改iFrame的HTML,解决方案是:必须使用。如果不能修改“”的代码,则不可能

如果可以,请尝试以下方法:

步骤1:加载iFrame时向其发送消息

$('#iframe').load(function(){
    source = $('iframe')[0].contentWindow;
    source.postMessage('hi', 'http://othersite.com:3000');
});
Setp 2:在“”上接收消息并修改textarea值

$(document).ready(function(){
  window.addEventListener("message", function(e){
    if(e.origin === 'http://yourwebsite'){
      $('.inp').val('put your text here');
    }
  }, false)
}
我认为这在第一时间是行不通的,请阅读文档并尝试,但保持不变:)
我希望这将对您有所帮助

如果承载iframe的页面没有从
othersite.com
提供服务(鉴于名称,这似乎不太可能),那么不,您根本无法与iframe页面交互;出于安全考虑,同源策略禁止此操作。如果
othersite.com
正在侦听消息,则您可以使用此功能。这是一种双向通信,他们必须具有侦听消息并将其插入文本区域的代码。