非法运输危险物品罪:Cross-Browser HTML5 Placeholder Text跨浏览器的HTML5的占位符文本

来源:百度文库 编辑:九乡新闻网 时间:2024/05/03 23:32:53

Cross-Browser HTML5 Placeholder Text

One of the nice enhancement in HTML5 web form is being able to addplaceholder text to input fields. Placeholder attribute allows you todisplay text in a form input when it is empty and when it is not focused(it clears the field on focus). This is a nifty feature, but it is notsupported by all browsers yet. This tutorial will show you how to useModernizr to detect if placeholder is supported, or else use jQuery todisplay the fallback placeholder text dynamically.

Demo HTML5 Placeholder

Download Demo Zip

Old School Javascript Way

Before we had the placeholder attribute, we relied on Javascript tofake the placeholder text. Below is an example. The text is inserted inthe value attribute. On focus, it checks if the value is "search" andreturns empty to clear the field. If the value is empty, it returns"search." As you can see, this way is not an efficient way because eachfield has to be checked.

jQuery Placeholder Text (Demo)

Now with HTML5 placeholder, it is more semantic to use placeholderthan value attribute. However, placeholder text is not supported by allbrowsers. To make it cross-browser, Modernizr and jQuery come in handy here.

Modernizr is used here to check if placeholder is supported. Ifplaceholder is not supported, the jQuery code will run. It finds allelements with placeholder attribute and stores in a variable. It thencompares the input value with the placeholder attribute. If the inputvalue is empty, it will display the placeholder text and add a"placeholder" class to the input field. View Demo.

To use this on your site, download a copy of Modernizr and jQuery andpaste the following code any where in your html page (be sure thejquery.js and modernizr.js file is in correct path).

Remove Webkit Search Input Styles

Webkit browsers add extra styling to the search input field. To remove them, add the following CSS code:

input[type=search] {
-webkit-appearance: none;
}input[type="search"]::-webkit-search-decoration,input[type="search"]::-webkit-search-cancel-button {display: none;}

Credit

The jQuery code is found at Nico Hagenburger.


  1. 网页
  2. 图片
  3. 视频
  4. 地图
  5. 新闻
  6. 购物
  7. Gmail
  8. 更多
    1. 翻译
    2. 图书
    3. 财经
    4. 学术搜索
    5. 博客
    6. 日历
    7. 照片
    8. 文档
    9. 阅读器
    10. 协作平台
    11. 论坛
    12. 更多 ?

Account Options

  1. xi8839@gmail.com 8839 xixi8839@gmail.com
    1. 创建个人资料
    2. 隐私权
    3. 帐户设置
    退出
    1. 帮助

翻译 请键入文字或网站地址,或者上传文档。 取消

将英语译成中文

跨浏览器的HTML5的占位符文本 Kuà liúlǎn qì de HTML5 de zhàn wèi fú wénběn 新! 点击上方的字词即可查看其他翻译。 删除

字典

“Google 翻译”适合以下用途:搜索手机聊天商务:译者工具包全球商机洞察Website Translator关于 Google 翻译关闭即时翻译隐私权政策帮助