【发布时间】:2018-10-15 05:12:26
【问题描述】:
与 iframe 问题相关的问题: Copy div from parent website to a textarea in iframe
我正在尝试将 InnerHtml 从 div 复制到 TextArea。
我在同一个网页上创建了两个谷歌翻译实例,我正在尝试将第一个实例的自动更正应用于第二个实例,而不更改第一个 textarea。
我尝试了不同的代码:
setInterval(function() {
childAnchors1 = document.querySelectorAll("#spelling-correction > a")[0];
$("#source")[1].val(childAnchors1.text());
}, 100);
setInterval(function copyText() {
$(".goog-textarea short_text")[1].val($("#spelling-correction > a")[0].innerText());
}
, 100);
setInterval(function copyText() {
$("#source")[1].val($("#spelling-correction > a")[0].innertext());
}
, 100);
setInterval(function() {
var finalarea = document.getElementsByClassName("goog-textarea short_text")[1];
var correction = document.querySelectorAll("#spelling-correction > a")[0].innerHTML
document.getElementsByClassName("goog-textarea short_text")[1].value = correction.innerText;
}, 100);
onclick='document.getElementsByClassName("goog-textarea short_text")[1].innerHTML=document.getElementById("spelling-correction > a")[0].innerHTML;'
但不幸的是,这似乎没有任何作用......
如果有任何帮助,我将不胜感激。
我应该提到这一点。我使用 iframe 创建第二个实例,所以简单的解决方案不起作用...... 这是我用于创建 iframe 实例的代码:
var makediv = document.createElement("secondinstance");
makediv.innerHTML = '<iframe id="iframenaturalID" width="1500" height="300" src="https://translate.google.com"></iframe>';
makediv.setAttribute("id", "iframeID");
var NewTranslator = document.getElementById("secondinstance");
var getRef = document.getElementById("gt-c");
var parentDiv = getRef.parentNode;
parentDiv.insertBefore(makediv, getRef);
我尝试使用它在 iframe 和父网站之间进行通信:
setInterval(function() {
var childAnchors1 = window.parent.document.querySelectorAll("#spelling-correction > a");
var TheiFrameInstance = document.getElementById("iframeID");
TheiFrameInstance.contentWindow.document.querySelectorAll("#source").value = childAnchors1.textContent;
}, 100);
但它不起作用......
好的,我让它工作了:
var a = document.createElement('iframe');
a.src = "https://translate.google.com";
a.id = "iframenaturalID";
a.width = "1000";
a.height = "500";
document.querySelector('body').appendChild(a)
和
let iframe = document.getElementById("iframenaturalID");
setInterval(function() {
let source = iframe.contentWindow.document.getElementById("source");
let destination = window.parent.document.querySelector("#spelling-correction > a");
source.value = destination.textContent;
}, 100);
现在它完成了我尝试做的事情,但是我仍然收到错误消息:Uncaught TypeError: Cannot set property 'value' of null
at eval,它指向这一行:source.value = destination.textContent;。虽然这不是什么大问题,但它返回这个错误仍然很奇怪......
好的,我可以通过添加setTimeout来解决它。
【问题讨论】:
-
查看我更新后的答案,该答案使用与定位元素相同的 HTML。
标签: javascript jquery html iframe selector