是否可以使用JavaScript将文本放入iFrame中的文本区域?
的index.html
<iframe src="http://othersite.com:3000">
在iFrame中有一个textarea
<textarea class="inp" placeholder="Send message..."></textarea>
是否可以将文字放入其中?
答案 0 :(得分:0)
您无法修改iFrame的HTML,解决方案:必须使用postMessage。如果您无法修改&#34; http://othersite.com:3000&#34;的代码这是不可能的。
如果可以的话,试试这个:
第1步:加载时向iFrame发送消息
$('#iframe').load(function(){
source = $('iframe')[0].contentWindow;
source.postMessage('hi', 'http://othersite.com:3000');
});
Setp 2:在&#34; http://othersite.com:3000&#34;上接收消息并修改textarea值
$(document).ready(function(){
window.addEventListener("message", function(e){
if(e.origin === 'http://yourwebsite'){
$('.inp').val('put your text here');
}
}, false)
}
我认为这在第一时间不起作用,阅读文档并尝试但保持相同的方式:) 我希望它会对你有所帮助