Greasemonkey (JavaScript) 文本区域问题
我编写了一个基本的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您在其中输入内容时,
textarea
的value
属性就会被设置。这会覆盖任何innerHTML
值。您应该使用
value
属性来设置输入元素(如textarea
)的内容。试试这个:
The
textarea
'svalue
attribute is set as soon as you type something into it. This overrides anyinnerHTML
value.You should be using the
value
attribute to set the contents of input elements liketextarea
.Try this instead:
如果您需要文本区域始终显示相同的文本,我建议您使用自定义的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 thetextarea
.If that's what you were after, 'cause I didn't understand it quite clearly what are you trying to do.