【问题标题】:Copy text from div innerHtml to textarea将文本从 div innerHtml 复制到 textarea
【发布时间】: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


【解决方案1】:

由于textarea 是一个表单元素,.innerText.innerHTML 都不起作用。您需要使用 value 属性(或使用 JQuery 的 .val())提取其内容。

仅供参考:

  • innerText,不是.innertext
  • .innerText 是属性,不是函数,所以不要在它后面加上()
  • 这是.innerHTML,而不是.innerHtml
  • innerHTML 用于字符串中应包含的 HTML 解析为 HTML 和 .textContent 用于不应该的字符串 被解析为 HTML。通常,您不会将其中的内容映射到 其他。
  • document.querySelectorAll() 扫描整个 DOM 以查找所有 匹配节点。如果您知道您只有一个匹配节点,或者您 只想要第一个匹配的节点,太浪费资源了。 而是使用.querySelector(),它会在 找到第一个匹配项。 由于您使用的是 JQuery,因此您应该在使用上保持一致。在 JQuery 中不需要 .querySelector().querySelectorAll(),只需使用 JQuery 选择器语法即可。

这是一个示例,它显示了使用您在问题中显示的 HTML 类型以及您显示的相同 id 值和嵌套结构的普通 JavaScript 和 JQuery 方法。您可以看到我使用不同的选择器来正确定位输入/输出元素。

// Standare DOM queries to get standard DOM objects
let source = document.getElementById("source");
let destination = document.querySelector("#spelling-correction > a");

// JQuery syntax to get JQuery objects:
let jSource = $("#source");
let jDestination = $("#spelling-correction > a");

// Vanilla JavaScript way to set up the event handler and do the work
source.addEventListener("keyup", function(){
  destination.textContent = source.value;  
});

// JQuery way to set up the event handler and do the work
jSource.on("keyup", function(){
  jDestination.text(jSource.val());   
});
textarea, div {
  border:3px solid grey;
  width:500px;
  height:75px;
  font-size:1.5em;
  font-family:Arial, Helvetica, sans-serif;
}

.destination { pointer-events:none; background:#e0e0e0; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Type in the first textarea</p>
<textarea id="source"></textarea>
<div id="spelling-correction">
  Did you mean: <a href="#"></a>
</div>

【讨论】:

  • 谢谢,现在我想出了如何用两个元素来做到这一点。但是,当我必须在第一个网站和同一网站的嵌入式 iframe 实例之间传递信息时,这不起作用。我将不得不研究如何做到这一点。
  • 谢谢,我让它与 Vanilla Javascript 代码一起工作。现在它做了我想要的。虽然它给了我错误消息(在source.value = destination.textContent; 行中,我在更新的答案中写了这个)。也许有一些方法可以摆脱这些错误;虽然它们不是很重要。谢谢!
【解决方案2】:

您尝试过的所有代码中的问题是如何正确获取文本。

从您的第一个示例开始,它应该使用innerText 而不是text(),因为它是一个jquery 对象并且您返回的是一个DOM 对象而不是一个jQuery 对象:

setInterval(function() {
    childAnchors1 = document.querySelectorAll("#spelling-correction > a")[0];
    $("#source")[1].val(childAnchors1.innerText);
}, 100);

在您的第二个示例和第三个示例中,您需要删除 innerText 中的括号,例如:

setInterval(function copyText() {
    $(".goog-textarea short_text")[1].val($("#spelling-correction > a")[0].innerText);
}, 100);

我建议使用纯js和textContent属性like:

setInterval(function() {
    var childAnchors1 = document.querySelectorAll("#spelling-correction > a")[0];
    document.querySelectorAll("#source")[1].value = childAnchors1.textContent;
}, 100);

注意:我应该指出您的 HTML 代码无效,因为当 id 属性在同一个文档中应该是唯一的时,您使用了重复的标识符。

【讨论】:

  • 我希望这就是你要找的@Ernest
  • 是的,HTML 代码无效,因为我在同一个网页上使用了两个谷歌翻译实例。不幸的是,似乎用 [0] 或 [1] 选择任何东西都不起作用。要么只选择页面上的第一个元素,要么只选择两者。
  • 我已经尝试了我的答案中的代码,它工作得很好
  • 也许问题是我使用了一个扩展来绕过 X-Frame 标头。因为当我尝试创建谷歌翻译的第二个实例时,它被写入它被拒绝。你是如何创建谷歌翻译的第二个实例的?
  • 二审是否也翻译了单词?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-11
  • 2014-08-22
  • 1970-01-01
相关资源
最近更新 更多