简体   繁体   English

更改 javascript 中 Textarea 的值

[英]Changing the value of a Textarea in javascript

I don't know how to change the value of data-initial-value (highlighted in red in the picture) with in the textarea div.我不知道如何在textarea div 中更改data-initial-value (图中以红色突出显示)。 I have tried using.innertext with the input "hi" but you can see that it only changes the inner text and has no effect on the value of data-initial-value .我已经尝试使用.innertext 输入“hi”,但您可以看到它只会更改内部文本并且对data-initial-value的值没有影响。 I am working on an extension for Google Meet and would like the type in the chat box and hit send.我正在开发 Google Meet 的扩展程序,并希望在聊天框中输入类型并点击发送。 So whenever something is typed into the chat box, Google Meet sets the value of the input to data-initial-value and not the innerText.因此,无论何时在聊天框中输入内容,Google Meet 都会将输入值设置为data-initial-value而不是 innerText。 So how do I change the value of data-initial-value ?那么如何更改data-initial-value呢?

Things I have tried that do not work:我尝试过但不起作用的事情:

document.querySelector(".KHxj8b").innerText = "hello world";
document.querySelector(".KHxj8b").value = "hello world";

Images Below:下图:

文本框中没有输入(以红色突出显示)


在文本框中输入“hello world”(以红色突出显示)

You will need to use setAttribute(), since data-initial-value is an attribute of that tag.您将需要使用 setAttribute(),因为 data-initial-value 是该标签的一个属性。

document.querySelector(".KHxj8b").setAttribute("data-initial-value", "The value");

Change "The value" to what ever you want it to be.将“值”更改为您想要的任何值。

You have to setAttribute('data-initial-value', yourvalue);你必须setAttribute('data-initial-value', yourvalue);

Use set attribute method: https://www.w3schools.com/jsref/met_element_setattribute.asp使用设置属性方法: https://www.w3schools.com/jsref/met_element_setattribute.asp

document.querySelector(".KHxj8b").setAttribute("data-initial-value", "The value"); document.querySelector(".KHxj8b").setAttribute("data-initial-value", "The value");

Also if it ain't working and you think you can delete that attribute then use JS to delete it.此外,如果它不起作用并且您认为可以删除该属性,则使用 JS 将其删除。 But it may break a few functionalities但它可能会破坏一些功能

element.removeAttribute(attributename)

Here the element is document.querySelector(".KHxj8b").这里的元素是 document.querySelector(".KHxj8b")。 and attribute is data-initial-value并且属性是数据初始值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM