带有描述性文本的文本输入
我想知道是否有一种简单的方法可以使用 javascript(包括我们在网站上使用的 JQuery)将描述性文本放入文本输入中,直到用户单击它来输入自己的文本。
例如,我想将“搜索”一词放入文本输入中(最好使用比实际输入更浅的颜色),直到用户单击输入,当它消失并允许他们输入搜索词时。
不过,我实际上并不希望“搜索”一词成为文本输入的值,因为用户可以搜索单词“搜索”有点重要。
我正在考虑绝对定位
在输入上包含单词搜索的元素,并在单击它(或输入)时将其隐藏。
你怎么认为? 这是严重的误导吗?
I was wondering if there's an easy way with javascript (including JQuery, which we're using on the site) to put descriptive text in a text input until the user clicks it to type in their own text.
For instance I'd like to put the word 'Search' in a text input (preferrably in a lighter color than real input) until the user clicks the input, when it disappears and allows them to type in their search terms.
I don't want to actually have the word 'Search' be the value of the text input though, because it is somewhat important that the user can search for the word search.
I was thinking of absolute positioning a <p> element with the word search over the input and hiding it when it (or the input) is clicked.
What do you think? is this horribly misguided?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我最近遇到了 jQuery 表单示例插件,它正是您想要的。 该项目的 github 页面位于此处。 使用该插件,您可以显示一个占位符值,该值将在单击时消失:
$('input#search').example('Search');
I recently bumped into the jQuery Form Example plugin that does just what you want. The github page for the project is here. With that plugin, you can display a placeholder value that will vanish on click with just this:
$('input#search').example('Search');
您可以将搜索输入的初始值设置为“搜索”,然后添加 onClick 侦听器以将其删除。
$("input.search-term").one("click", function() { $(this).removeAttr("value"); });
不会影响用户的搜索“搜索”的能力。 与尝试在输入上欺骗
或
元素相比,这是一种更简洁的方法。
编辑:你们都绝对正确 - 删除每次点击的价值是糟糕的用户体验。 这就是我快速回答的结果。 :) 更新为仅删除初始值。
You can set the initial value of the search input to "Search" and then add an onClick listener to remove it.
$("input.search-term").one("click", function() { $(this).removeAttr("value"); });
It won't impact the user's ability to search for "search." This is a much cleaner approach than trying to finagle a
<p>
or<label>
element over the input.EDIT: You are all absolutely right - removing the value on each click is poor UX. That's what I get for answering quickly. :) Updated to only remove the initial value.
您可能想检查一下:
http://www.newmediacampaigns.com/page/nmcformhelper
基本上,HTML 5 支持输入的 placeholder 属性elements:
WebKit(Chrome 和 Safari)现在支持此功能,但对于 Firefox 和 IE 等其他浏览器,当浏览器不支持 HTML 5 功能时,您可以使用此脚本使用 Javascript 来模拟效果。
You may want to check this out:
http://www.newmediacampaigns.com/page/nmcformhelper
Basically, HTML 5 supports the placeholder attribute for input elements:
WebKit (Chrome and Safari) supports this now, but for other browsers like Firefox and IE, you can use this script to simulate the effect using Javascript when the browser doesn't support the HTML 5 feature.
多年来,有一个名为 labels.js,最初由老 youngpup.net 的 Aaron Boodman 编写。 这个想法仍然存在,甚至存在一个 jQuery 版本(演示#4)。
For years there has been a popular "project" known as labels.js, orignally written by Aaron Boodman of the old youngpup.net. The idea is still around and there exists a jQuery version, even (demo #4).
在这种情况下,您可以在文本输入上使用背景图像。 您可以在 CSS 中定义:焦点不应该有背景,而未聚焦的应该有背景。 即使禁用 javascript,此解决方案也能工作。
更新:已测试并适用于 FF、Opera 和 Chrome,但不适用于 IE,因为 IE 不支持 :focus,但 IE 支持背景图像,所以如果文本是浅灰色的,应该不是问题。 您始终可以使用 jquery 来更改输入字段应具有的类别。
In that case you could use a background-image on the text-input. You could define in CSS that :focus should not have the background and that the unfoucused should have it. This solution works even if javascript is disabled.
Update: Tested and work with FF, Opera and Chrome but not IE, since IE don't support :focus, but IE supports the background image, so if the text is light gray is should not be a problem. And you can always use jquery to change what classed the input field should have.
我不久前一直在寻找这个解决方案,并遇到了这个
I was looking for this solution not long ago and came cross this
我会在 jQuery 中做类似的事情:
这样用户再次单击时就不会丢失之前输入的内容。 仅当该值是您分配给输入字段的默认值时才会被清除。
I would do something like this in jQuery:
This way users won't lose what they have typed previously upon clicking again. The value would only be cleared if it is the default value you assign to the input field.
作为参考,这种技术通常称为水印。
For reference, this technique is often called watermarking.