Javascript“水印”对于文本框

发布于 2024-08-14 09:04:20 字数 134 浏览 7 评论 0原文

我想知道如何在页面加载时将文本放入文本框中。示例:如果我有一个登录页面,我将有 2 个名为“用户名”和“密码”的文本框。当页面加载时,我希望文本框加载其中写有“用户名”的内容,因此我不必在外面放置标签。但是当用户单击文本框内部时,它应该消失。我该怎么做?

I would like to know how to put text in a textbox when the page loads. Example: If I have a login page, I would have 2 textboxes named "username" and "password". When the page loads I want the the textbox to load with "username" written inside, so I won't have to put a label for that outside. But when the user clicks inside textbox it should disppear. How do I do this?

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

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

发布评论

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

评论(6

故笙诉离歌 2024-08-21 09:04:20
 <input name="q" onfocus="if (this.value=='search') this.value = ''" type="text" value="search"> 

...来自 Stack Overflow 搜索框。


您还可以添加 onblur 事件来检查: if (this.value=='') this.value = 'search'

这会在以下情况下重新打印水印:用户在文本框外部单击,该字段为空。

 <input name="q" onfocus="if (this.value=='search') this.value = ''" type="text" value="search"> 

...from the Stack Overflow search box.


You could also add the onblur event to check: if (this.value=='') this.value = 'search'

This would re-print the watermark when the user clicks outside the textbox and the field is empty.

花期渐远 2024-08-21 09:04:20

更现代的方法是使用“PlaceHolder”

<input type="text" placeholder="search" />

A more modern way is to use "PlaceHolder"

<input type="text" placeholder="search" />
为你鎻心 2024-08-21 09:04:20

有很多关于如何执行此操作的教程。 这是使用 jQuery JavaScript 框架的一个演练

这是另一篇热门博客文章 关于它,只需使用常规的 javascript。

There are a lot of tutorials on how to do this. Here's one walkthrough using the jQuery javascript framework.

Here's another popular blog post about it, just using regular javascript.

不必了 2024-08-21 09:04:20

通俗地说:

  • 当聚焦于输入时,如果值为“用户名”,则将其设置为“”
  • 当从框中移开焦点时,如果值为“”(即没有输入任何内容),则将其重置为“用户名”仍然向用户提供反馈,因为他们尚未输入数据

代码:

<input value="Username" onfocus="if(this.value=='Username') this.value = ''" onblur="if(this.value=='') this.value = 'Username'" type="text" />

In laymen's terms:

  • When focusing on the input, if the value is "Username" then set it to ""
  • When removing focus from the box, if the value is "" (i.e. nothing was entered), reset it to "Username" to still provide feedback to the user since they haven't yet entered data

The code:

<input value="Username" onfocus="if(this.value=='Username') this.value = ''" onblur="if(this.value=='') this.value = 'Username'" type="text" />
去了角落 2024-08-21 09:04:20

JS:

   function clearDefault(ctl){
       if(ctl.value==ctl.defaultValue) { ctl.value = ""; }
    }

在您的文本框中包含 onfocus="clearDefault(this)" 并将其文本设置为“用户名”或“密码”。

JS:

   function clearDefault(ctl){
       if(ctl.value==ctl.defaultValue) { ctl.value = ""; }
    }

In your textbox include onfocus="clearDefault(this)" and set its text to "username" or "password".

瑾兮 2024-08-21 09:04:20

请参阅这篇文章 http://www.alistapart.com/articles/makingcompactformsmoreaccessible。当然,如果您使用 jQuery,则有“Label over”插件。

See this article http://www.alistapart.com/articles/makingcompactformsmoreaccessible. Of course, if you are using jQuery, there are "Label over" plugins.

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