博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
属性占位符_HTML5的占位符属性
阅读量:2512 次
发布时间:2019-05-11

本文共 1873 字,大约阅读时间需要 6 分钟。

属性占位符

HTML5 has introduced many features to the browser;  some HTML-based, some in the form of JavaScript APIs, but all of them useful.  One of my favorites if the introduction of the placeholder attribute to INPUT elements.  The placeholder attribute shows text in a field until the field is focused upon, then hides the text.  You've seen this technique a billion times with JavaScript, but native HTML5 support is even better!

HTML5为浏览器引入了许多功能。 有些基于HTML,有些采用JavaScript API的形式,但它们都非常有用。 如果将placeholder属性引入INPUT元素,则是我的最爱之一。 placeholder属性在字段中显示文本,直到该字段被关注,然后隐藏该文本。 您已经使用JavaScript看到了十亿次这种技术,但是原生HTML5支持甚至更好!

HTML (The HTML)

 

You'll notice that all you need to do is add a placeholder attribute with the generic text of your choice.  Nice that you don't need JavaScript to create this effect, huh?

您会注意到,您需要做的就是添加一个placeholder属性以及您选择的通用文本。 很好,您不需要JavaScript即可创建这种效果,是吗?

检查占位符支持 (Checking for Placeholder Support)

Since placeholder is a new capability, it's important to check for support in the user's browser:

由于placeholder是一项新功能,因此检查用户浏览器中的支持很重要:

function hasPlaceholderSupport() {	var input = document.createElement('input');	return ('placeholder' in input);}

If the user's browser doesn't support the placeholder feature, you will need to employ a fallback with MooTools, Dojo, or the JavaScript toolkit of your choice:

如果用户的浏览器不支持placeholder功能,则需要使用MooTools,Dojo或您选择JavaScript工具包进行后备管理:

/* mootools ftw! */var firstNameBox = $('first_name'),	message = firstNameBox.get('placeholder');firstNameBox.addEvents({	focus: function() {		if(firstNameBox.value == message) { searchBox.value = ''; }	},	blur: function() {		if(firstNameBox.value == '') { searchBox.value = message; }	}});

placeholder is another great addition to the browser and replaces the oldest snippet of JavaScript in the book! You can event !

placeholder是浏览器的另一个重要补充,它取代了本书中最古老JavaScript代码段! 您可以使用事件 !

翻译自:

属性占位符

转载地址:http://vgswd.baihongyu.com/

你可能感兴趣的文章
2015年创业中遇到的技术问题:21-30
查看>>
《社交红利》读书总结--如何从微信微博QQ空间等社交网络带走海量用户、流量与收入...
查看>>
JDK工具(一)–Java编译器javac
查看>>
深入.NET框架与面向对象的回顾
查看>>
改变label中的某字体颜色
查看>>
七牛云存储之应用视频上传系统开心得
查看>>
struts2日期类型转换
查看>>
Spark2-数据探索
查看>>
Http和Socket连接区别
查看>>
Angular2,Springboot,Zuul,Shiro跨域CORS请求踩坑实录
查看>>
C语言中操作符的优先级大全
查看>>
pgpool-II - 介绍
查看>>
Alpha冲刺(10/10)——2019.5.2
查看>>
图书管理系统用例
查看>>
Microsoft patterns & practices 学习笔记(0)
查看>>
在腾讯云上创建您的SQL Cluster(4)
查看>>
部署在腾讯云的公益网站遭受了一次CC攻击
查看>>
linux ping命令
查看>>
Activiti源码浅析:Activiti的活动授权机制
查看>>
数位dp整理
查看>>