如何使用 JavaScript 将文本插入表单
我正在尝试创建一个供个人使用的简单扩展。部分是因为懒惰,部分是因为学习的冲动。我以前从未进行过扩展,但我一直在查看文档。现在我只需要编写代码。我想做的是,当浏览器加载某个页面时,将文本插入特定的表单中。形式如下
<div id="set_tags" class="advanced_option">
<label for="post_tags" class="inline_input_label" id="post_tags_label"
onclick="Element.remove($(this))"
style="left:8px; right:auto; text-align:left">tags</label>
<input id="post_tags" name="post[tags]" type="text"/>
</div>
我没有太多使用javascript,所以有没有办法在页面加载时向其中添加文本“音乐”?
I'm trying to create a simple extension for personal use. It's partially from laziness, and partially from an urge to learn. I've never made extensions before, but I've been looking at the documentation. Now I just need to write the code. What I'm trying to do, is when the browser loads a certain page, to insert text into a specific form. The form is as follows
<div id="set_tags" class="advanced_option">
<label for="post_tags" class="inline_input_label" id="post_tags_label"
onclick="Element.remove($(this))"
style="left:8px; right:auto; text-align:left">tags</label>
<input id="post_tags" name="post[tags]" type="text"/>
</div>
I haven't worked much with javascript, so is there a way to add the text "Music" to this when the page is loaded?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
onload
函数来启动您的函数。http://javascript.about.com/library/blonload.htm
由于您是新用户对于 javascript,您可能想熟悉
unobtrusive javascript
(http: //www.onlinetools.org/articles/unobtrusivejavascript/chapter4.html),我发现这是编写 javascript 的更好方法,因为您可以轻松注释掉 javascript 并查看它在禁用时如何工作。但是,一开始学习这个会更容易。要获取
input
标记,您可以使用document.getElementById()
,如下所示:然后,要向此字段添加文本,应该有一个
value<上面的
input
定义中的 /code> 属性,您只需执行以下操作:You can use the
onload
function to start your function.http://javascript.about.com/library/blonload.htm
Since you are new to javascript you may want to get familiar with
unobtrusive javascript
(http://www.onlinetools.org/articles/unobtrusivejavascript/chapter4.html) which I find is a better way to write javascript, as you can then easily comment out javascript and see how it works when that is disabled. But, it would be easier to learn this in the beginning.To get the
input
tag you can usedocument.getElementById()
which would be something like:Then, to add text to this field there should be a
value
property in yourinput
definition above, and you would just do:我假设您想将其放在元素
post_tags_label
的末尾。I'm assuming that you want to put it at the end of the element
post_tags_label
.如果您使用 GreaseMonkey,这真的很容易做到。它非常适合您想要对网页等进行的个人更改。
This is really easy to do if you use GreaseMonkey. It's perfect for personal changes you want to make to web pages, etc.