使用 google chrome 扩展的 javascript 将文本插入到 textarea 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26346956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inserting text into textarea with javascript for google chrome extension
提问by DCarrollUSMC
I am trying to extend the functionality of an existing google chrome extension. Using the Wrike google chrome extension, my goal is to add a button (or buttons) which will add some text to the description field (a textarea). The desired effect will be that if a user clicks an "Add Template" button, the code/text will be inserted into the textarea with id="description" which is native to the Wrike chrome extension. Below you will find some of the code that I have been working with.
我正在尝试扩展现有 google chrome 扩展程序的功能。使用 Wrike google chrome 扩展程序,我的目标是添加一个按钮(或多个按钮),它将向描述字段(文本区域)添加一些文本。预期的效果是,如果用户单击“添加模板”按钮,代码/文本将插入到带有 id="description" 的 textarea 中,这是 Wrike chrome 扩展的原生。您将在下面找到我一直在使用的一些代码。
Here is the description part of the form. Located in createTask.html
这是表格的描述部分。位于 createTask.html
<div class="main-description main-row">
<textarea placeholder="Click to add description" id="description" class="main-description-text"></textarea>
</div>
This is the button that I created which will initiate adding the text to the textarea:
这是我创建的按钮,它将开始将文本添加到 textarea:
<span class="btn m-green btn-addtemp" id="link">Add Template</span>
Within template.js (which is properly linked to as an external .js file within createTask.html:
在 template.js 中(在 createTask.html 中作为外部 .js 文件正确链接到:
function insertText(text) {
document.getElementById("description").innerHTML = text;
}
document.addEventListener('DOMContentLoaded', function() {
var link = document.getElementById('link');
link.addEventListener('click', function() {
insertText('Sample text here');
});
});
I can get this code to insert the 'Sample text here' into a div with an ID, but cannot get it to insert into the textarea with id=description. Any help would be greatly appreciated and I am more than happy to provide more detailed information if necessary. Thanks!
我可以使用此代码将“此处的示例文本”插入带有 ID 的 div,但无法将其插入带有 id=description 的文本区域。任何帮助将不胜感激,如有必要,我非常乐意提供更详细的信息。谢谢!
回答by Héctor
Try with element.value:
尝试使用 element.value:
function insertText(text) {
document.getElementById("description").value= text;
}
回答by Thibault Bach
To insert into textarea, use value properties :
要插入文本区域,请使用值属性:
function insertText(text) {
document.getElementById("description").value= text;
}
instead of
代替
function insertText(text) {
document.getElementById("description").innerHTML = text;
}
回答by Pinkponyprincess
"which will add some HTML tables and text to the description field (a textarea). ", this is not posible, textareas can not contain html.
“这将在描述字段(一个文本区域)中添加一些 HTML 表格和文本。”,这是不可能的,文本区域不能包含 html。