如何使用 JavaScript 将文本插入表单

发布于 2024-10-01 18:07:00 字数 515 浏览 6 评论 0原文

我正在尝试创建一个供个人使用的简单扩展。部分是因为懒惰,部分是因为学习的冲动。我以前从未进行过扩展,但我一直在查看文档。现在我只需要编写代码。我想做的是,当浏览器加载某个页面时,将文本插入特定的表单中。形式如下

<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 技术交流群。

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

发布评论

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

评论(3

月竹挽风 2024-10-08 18:07:01

您可以使用onload函数来启动您的函数。
http://javascript.about.com/library/blonload.htm

由于您是新用户对于 javascript,您可能想熟悉 unobtrusive javascript (http: //www.onlinetools.org/articles/unobtrusivejavascript/chapter4.html),我发现这是编写 javascript 的更好方法,因为您可以轻松注释掉 javascript 并查看它在禁用时如何工作。但是,一开始学习这个会更容易。

要获取 input 标记,您可以使用 document.getElementById() ,如下所示:

var elem = document.getElementById('post_tags');

然后,要向此字段添加文本,应该有一个 value<上面的 input 定义中的 /code> 属性,您只需执行以下操作:

elem.value = "Music";

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 use document.getElementById() which would be something like:

var elem = document.getElementById('post_tags');

Then, to add text to this field there should be a value property in your input definition above, and you would just do:

elem.value = "Music";
浪漫人生路 2024-10-08 18:07:01
document.getElementById("post_tags_label").appendChild(
    document.createTextNode("Music"));

我假设您想将其放在元素 post_tags_label 的末尾。

document.getElementById("post_tags_label").appendChild(
    document.createTextNode("Music"));

I'm assuming that you want to put it at the end of the element post_tags_label.

落墨 2024-10-08 18:07:01

如果您使用 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.

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