- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
与 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
处,它指向这一行:
在 evalsource.value = destination.textContent;
。虽然这不是一个大问题,但它返回这个错误仍然很奇怪......
好的,我可以通过添加 setTimeout
来解决它。
最佳答案
因为 textarea
是一个表单元素,所以 .innerText
或 .innerHTML
都不起作用。您需要使用 value
属性(或使用 JQuery 的 .val()
)提取其内容。
仅供引用:
innerText
,不是.innertext
。.innerText
是一个属性,而不是一个函数,因此您不要在它后面使用 ()
。.innerHTML
,不是.innerHtml
。innerHTML
当字符串中有 HTML 时使用解析为 HTML 和 .textContent
用于不应该的字符串被解析为 HTML。通常,您不会将 one 的内容映射到其他。document.querySelectorAll()
扫描整个 DOM 以找到所有匹配节点。如果你知道你只有一个匹配节点或者你只想要第一个匹配的节点,那是一种资源浪费。相反,使用 .querySelector()
,它会在找到第一个匹配项。由于您使用的是 JQuery,因此您应该在使用上保持一致。 JQuery 不需要 .querySelector()
或 .querySelectorAll()
,只需使用 JQuery 选择器语法即可。这是一个示例,它显示了普通 JavaScript 和 JQuery 方法,它们使用您在问题中显示的 HTML 类型,具有与您显示的相同 id
值和嵌套结构。您可以看到我使用了不同的选择器来正确定位输入/输出元素。
// 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>
关于javascript - 将文本从 div innerHtml 复制到 textarea,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52669661/
我有一个非常简单的函数,用于替换元素的innerHTML。我已经尝试调试这个问题几个小时了,但就是做不到,这令人恼火。 当从按钮调用时,按下 JavaScript(如下)可以正常工作,但是当从另一个函
我正在开发的代码片段有四个带有 javascript 的内部 html,现在我的问题是我们能否从所有这些数据中获取所有这些数据并添加(如果是整数)它们或连接(如果是字符串)并显示在另一个 div 标签
我正在使用 [innerHTML]显示一个字符串。字符串由同一对象的两个属性组成。该对象来自将对象列表(来自 NgRx 的 Observable)传递给 *ngFor .此外,管道用于决定应该在 [i
首先,我对编码完全陌生,并且一直在空闲时间使用自学工具学习 Javascript。我已经学到了足够的知识来开始构建自己的项目。我的第一次尝试是构建一个随机发生器(在本例中为随机餐厅名称)。 Javas
如题,这些span元素在浏览器中以两种样式显示,为什么? function loadHTML() { var html = 'sfdssfds';
我有一个格式如下的 HTML 文件: subject detail important subject detailimportant 我写了一个 PHP 代码来自动获取每个 p1 并将它们插入到我的
我希望这个主题符合问题。 嘿,请原谅我的笨蛋,但我一直在绞尽脑汁地试图解决这个问题。 代码: chapter 1';">Wonderful 我想要的是显示一个名为“Wonderful”的链接,
我正在调用一个打印到 div 的函数,然后返回一个也打印到 div 的字符串。以下将只打印“二”,但我期待它先打印“一”再打印“二”: global_cdiv = "view" fu
我有一个 不是 contentEditable 的 div。我捕获击键,将关联的字符插入到内存中的字符串中,然后调用 render() 函数用当前字符串替换 div 的 innerHTML。 我的问题
假设我们在页面上有一个 DIV x,我们想将那个 DIV 的内容复制(“复制粘贴”)到另一个 DIV y 中。我们可以这样做: y.innerHTML = x.innerHTML; 或使用 jQuer
我正在尝试根据 javascript 函数填充的数字更改 div 的innerHTML。不幸的是,我收到了一个错误,但我不确定为什么。 伪代码 如果number > 2将innerHTML更改为秒,否
我正在使用 ServiceNow 和 Angular.js 构建的网站上进行开发。页面似乎工作正常,直到我将 body 替换为自身后,所有 Buttons/onClicks 或搜索都停止响应..有人知
带有 [innerHtml] 指令的元素似乎只是在该元素内添加声明的 html 字符串,仅此而已; 通过 this stackblitz ,我试图在这样的元素中添加一些东西,但没有成功; new wo
为什么选址 http://xn--wiadomesny-37b.pl/test/抛出 Uncaught TypeError: Cannot set property 'innerHTML' of nu
我的 javascript 获取日期和时间并将其放置在 div 中如下: function print(message){ var div1= document.getElementById("d
在下面的代码中,我尝试以不将猫名称硬编码到 html 的方式设置猫名称。因此我使用的是数组。然而,每当我尝试将innerHTML属性设置为catNames [0]或catNames [1]时,我都会收
我正在使用 mootool 的 Request.JSON 从 Twitter 检索推文。收到它后,我将写入目标 div 的 .innerHTML 属性。当我在本地将其作为文件进行测试时,即 file:
这个问题在这里已经有了答案: How to get Angular2 to bind component in innerHTML (1 个回答) 关闭 6 年前。 所以我正在构建一个 angula
我有一个简短的脚本,它在 innerHTML 中查找具有特定文本的特定类,然后使用 replaceWith 替换整个元素。当只有一段特定的文本时,此方法非常有用,但我有几个项目要查找和替换。 下面的
我正在尝试使用 Wordpress/Woocommerce 上动态生成的 div 类更改“查看购物车”按钮的 innerHTML。我之前问过一个关于这个的问题,有人建议(谢谢 Mike :))因为 J
我是一名优秀的程序员,十分优秀!