如何在 Firefox 和其他不支持 html5 标签选项的浏览器中获取占位符文本?
这适用于 Chrome 和任何其他支持 HTML5 中占位符文本的浏览器,
<input id="name" name="name" type="text" placeholder="Please enter your name..." required /> <br />
但是,它不适用于 3.5 及更早版本的 Firefox,显然还有 IE8,可能还有其他浏览器。
我如何实现同样的目标(最好是在 HTML/CSS 中 - 如果不是,我愿意接受建议),以支持所有旧版浏览器?如果不是每个浏览器,至少是 Firefox 和 IE。
Safari 和 Chrome 已经支持它(或者最新版本)。
谢谢。
This works in Chrome and any other browser that supports placeholder text in HTML5
<input id="name" name="name" type="text" placeholder="Please enter your name..." required /> <br />
But, it doesn't work in 3.5 and earlier of Firefox, and obviously IE8, and possibly other browsers.
How do I achieve the same thing (preferably in HTML/CSS - if not I am open to suggestions), to support all the older browsers? If not every single browser, at least Firefox and IE.
Safari and Chrome already support it (or the latest versions anyway).
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
有一天我会抽出时间来正确记录这一点,但请参阅以下示例: http: //dorward.me.uk/tmp/label-work/example.html
简而言之 - 将
放置在透明
下> 使用
提供背景颜色和边框。
然后用JS根据焦点来判断标签是否可见。
当 JS 无法将标签放置在元素旁边时,应用不同的样式。
与使用
值
不同,这不会导致仅显示焦点内容的设备(例如屏幕阅读器)无法访问内容,并且也适用于密码
类型的输入。One day I'll get around to properly documenting this, but see this example: http://dorward.me.uk/tmp/label-work/example.html
In short — position a
<label>
under a transparent<input>
using<div>
to provide background colour and borders.Then use JS to determine if the label should be visible or not based on focusing.
Apply different styles when JS is not available to position the label beside the element instead.
Unlike using the
value
, this doesn't render the content inaccessible to devices which only display the focused content (e.g. screen readers), and also works for inputs of thepassword
type.我用这个:https://github.com/danbentley/placeholder
轻量且简单的 jQuery 插件。
I use this one: https://github.com/danbentley/placeholder
Lightweight and simple jQuery plugin.
这是我发现在任何地方都适用的最简单的解决方案:
将 PLACEHOLDER 替换为您自己的。
目前,FF3 尚不支持“input”元素的“placeholder”属性。 FF4、Opera11和Chrome8部分支持它,即它们在字段中渲染占位符文本,但当用户聚焦在字段中时不删除它,这比根本不支持更糟糕。
Here is the simplest solution that I found working everywhere:
Replace PLACEHOLDER with your own.
At the moment, FF3 does not yet support the "placeholder" attribute of the "input" element. FF4, Opera11 and Chrome8 support it partially, i.e. they render the placeholder text in the field, but do not delete it when the user focuses in the field, which is worse that not supporting it at all.
我使用用 jQuery 编写的以下代码片段。只需将一类 textbox-auto-clear 添加到页面上的任何文本框即可。
我假设您希望继续使用 HTML5 浏览器的占位符属性,在这种情况下,您必须进行一些浏览器检测,并且仅将 jQuery 解决方案应用于不支持它的浏览器。
更好的是,您可以使用 Modernizer 库,如本答案所述。
通过 jQuery 检测对特定 HTML 5 功能的支持< /a>
I use the following snippet that I wrote with jQuery. Just add a class of textbox-auto-clear to any textbox on the page and you should be good to go.
I assume that you want to keep using the placeholder attribute for HTML5 browsers, in which case you'd have to do some browser detection and only apply the jQuery solution to browsers that don't support it.
Better yet, you can us the Modernizer library, as outlined in this answer.
Detecting support for specific HTML 5 features via jQuery
这是一个 MooTools 插件,它将占位符带到尚不支持它的浏览器:
http://mootools .net/forge/p/form_placeholder
Here is a MooTools Plugin, that brings the placeholder to browsers that don't support it yet:
http://mootools.net/forge/p/form_placeholder
我用这个: https://github.com/Jayphen/placeholder
这个轻量且简单的 jQuery 插件是 danbentley/placeholder 的一个分支。
优点:它为临时填充的输入字段添加了一个“占位符”类。
CSS“.placeholder {color:silver}”使填充文本看起来像占位符,而不是常规输入文本。
缺点:它不会填充密码字段的占位符。
I use this one: https://github.com/Jayphen/placeholder
This lightweight and simple jQuery plugin is a fork of danbentley/placeholder.
Advantage: it adds a class "placeholder" to input fields that are temporarily filled.
Css ".placeholder {color:silver}" make the polyfill text look like a placeholder instead of regular input text.
Disadvantage: It doesn't polyfill the placeholder of a password field.
顺便说一句...如果有人感兴趣...我发现了一个很好的优雅的解决方案,它是一个非常棒的 jQuery 插件。
它实际上是一行 jQuery,一个缩小的 js 插件,以及输入上的一个简单的类名。
http://labs.thesedays.com/projects/jquery/clearfield/ 我发现的最美丽的东西,仅次于 html 中的“Placeholder”。
By the way...if anyone is interested...I found a nice elegant solution that is a jQuery plugin that is SOOO nice.
It literally is one line of jQuery, a minified js plugin, along with a simple class name on the input.
http://labs.thesedays.com/projects/jquery/clearfield/
It's the most beautiful thing I have discovered, next to 'Placeholder' in html.
诀窍是使用 javascript 函数 onBlur() 和 onFocus()。
以下是对我有用的代码:
SUBSCRIPTION_HINT (即:“您的电子邮件”)和 SUBSCRIPTION_LINK (即:EN mailchimp 嵌入代码中“action”标记的值...)是用于本地化的 PHP 常量。
The trick is to use javascript functions onBlur() and onFocus().
Here is the code that worked for me:
SUBSCRIPTION_HINT (i.e.: "your e-mail" ) and SUBSCRIPTION_LINK (i.e.: the value of the 'action' tag in your EN mailchimp embed code...) are PHP constants used for localization.
对于 Firefox 中的“占位符”工作,只需添加属性
CSS 标签中的
For "placeholder" work in Firefox just add the attribute
in CSS tags.
对我有用,将你的 CSS 更改为
Works for me, change your CSS to