Greasemonkey (JavaScript) 文本区域问题

发布于 2024-08-07 18:24:33 字数 385 浏览 2 评论 0原文

我编写了一个基本的 Greasemonkey 脚本,它将输出传递到 textarea,如下所示:

var obj = document.getElementById("comment").innerHTML = "\n" + "Note:" + "\n" + "\n" + output_string;

它的工作原理就像一个魅力,如果您更改源中的值,它将更新文本区域。但是,一旦您自己在文本区域内写入任何内容并选择一个值,它就不会覆盖您在文本区域内写入的内容。并且您需要完全刷新页面才能再次使用该功能。这是为什么?

I have written a basic Greasemonkey script which passes an output out to a textarea, like this:

var obj = document.getElementById("comment").innerHTML = "\n" + "Note:" + "\n" + "\n" + output_string;

It works like a charm, if you change the value from the source, it will update the textarea. However, as soon as you write anything yourself inside the textarea and select a value, it will not overwrite what you written inside the textarea. And you need to refresh the page entirely to be able to use the function again. Why is that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

成熟稳重的好男人 2024-08-14 18:24:33

当您在其中输入内容时,textareavalue 属性就会被设置。这会覆盖任何 innerHTML 值。

您应该使用 value 属性来设置输入元素(如 textarea)的内容。

试试这个:

var obj = document.getElementById("comment").value = "\n" + "Note:" + "\n" + "\n" + output_string;

The textarea's value attribute is set as soon as you type something into it. This overrides any innerHTML value.

You should be using the value attribute to set the contents of input elements like textarea.

Try this instead:

var obj = document.getElementById("comment").value = "\n" + "Note:" + "\n" + "\n" + output_string;
丢了幸福的猪 2024-08-14 18:24:33

如果您需要文本区域始终显示相同的文本,我建议您使用自定义的greasemonkey 脚本在其上附加模糊事件。模糊事件将执行与现在相同的操作。唯一的变化是,当光标从 textarea 上聚焦时,textarea 将被更新。

如果这就是你想要的,因为我不太清楚你想做什么。

If you need your text area to always display the same text I suggest you attach blur event on it using your customized greasemonkey script. Blur event would do the same thing as it does now. The only change would be that textarea will be updated the moment cursor focuses off the textarea.

If that's what you were after, 'cause I didn't understand it quite clearly what are you trying to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文