<input id="username" name="username" A detailed introduction to the HTML5 Placeholder attribute="请输入用户名" type="text">
<textarea>
multi-line text box and type
attribute value is text, password, search, tel, url or email, etc.<input> ;
. <p>::A detailed introduction to the HTML5 Placeholder attribute
pseudo-element selector, but There is currently no browser support, so it can only be defined separately according to the different implementation methods of different browsers: ::-webkit-input-A detailed introduction to the HTML5 Placeholder attribute { /* Chrome/Safari/Opera */ color: green;}::-moz-A detailed introduction to the HTML5 Placeholder attribute { /* Firefox 19+ */ color: green;}:-ms-input-A detailed introduction to the HTML5 Placeholder attribute { /* IE 10+ 注意这里只有一个冒号 */ color: green;}
::-webkit-input-A detailed introduction to the HTML5 Placeholder attribute,::-moz-A detailed introduction to the HTML5 Placeholder attribute { color: green;}
:-moz-A detailed introduction to the HTML5 Placeholder attribute { /* Firefox 4 - 18 */ color: green;}
:-moz-A detailed introduction to the HTML5 Placeholder attribute
is abandoned and switched to the pseudo-element definition method with two colons. At the same time, it also adds a default opacity: 0.54
opacity style, which can be overridden if necessary, otherwise the text is translucent: ::-moz-A detailed introduction to the HTML5 Placeholder attribute { color: green; opacity: 1;}
:first-child
Pseudo-class, select the first child element: p:first-child { font-size: 16px;}
p.first-child { font-size: 16px;}
<p>
element . <p>The pseudo element can be understood as adding a virtual element. For example, the p:before
pseudo element can be understood like the following pseudocode: <before>p:before</before><p>paragraph</p>
<p>
element and p:before
can be understood as two different elements. If you are confused, it doesn’t matter. After all, this is not the focus of this article. For more information about pseudo-elements and pseudo-classes, please refer to Pseudo-classes - CSS | MDN and Pseudo-elements - CSS | MDN:-ms-input-A detailed introduction to the HTML5 Placeholder attribute
selector to define the style of A detailed introduction to the HTML5 Placeholder attribute. In fact, the style is applied to the text input box Yes, if there are other more specific style rules for the selector of the text input box, this style will be overwritten. Refer to the following code: input:-ms-input-A detailed introduction to the HTML5 Placeholder attribute { /* 0, 0, 1, 1 */ color: green;}#username { /* 0, 1, 0, 0 */ color: blue;}
!important
rules that can be used. Other browsers that use pseudo-element selectors with two colons will not have this problem, for example: input::-webkit-input-A detailed introduction to the HTML5 Placeholder attribute { /* 0, 0, 0, 2 */ color: green;}#username { /* 0, 1, 0, 0 */ color: blue;}
:focus
pseudo-class selector to set the text color of the A detailed introduction to the HTML5 Placeholder attribute to transparent: :focus::-webkit-input-A detailed introduction to the HTML5 Placeholder attribute { color: transparent;}
A detailed introduction to the HTML5 Placeholder attribute
attribute of the corresponding text input box: $('input').attr('A detailed introduction to the HTML5 Placeholder attribute', 'Please enter your name');
window.getComputedStyle()
method to get its style attributes. The second parameter of this method is A pseudo element: window.getComputedStyle(document.getElementById('username'), '::-moz-A detailed introduction to the HTML5 Placeholder attribute').getPropertyValue('color'); // "rgb(0, 255, 0)"
.style-1::-moz-A detailed introduction to the HTML5 Placeholder attribute { color: green;}.style-2::-moz-A detailed introduction to the HTML5 Placeholder attribute { color: red;}
class
属性来实现修改样式的目的:$('input').addClass('style-2').removeClass('style-1');
<input>
元素的 A detailed introduction to the HTML5 Placeholder attribute
属性,可以引入 Modernizr 库来判断:if (!Modernizr.input.A detailed introduction to the HTML5 Placeholder attribute) { // 做点什么事}
<input>
元素对象,并判断该元素对象是否具有 A detailed introduction to the HTML5 Placeholder attribute
属性:'A detailed introduction to the HTML5 Placeholder attribute' in document.createElement('input')
<textarea>
元素也是一样:'A detailed introduction to the HTML5 Placeholder attribute' in document.createElement('textarea')
({}).toString.call(window.operamini) === '[object OperaMini]'
if (!('A detailed introduction to the HTML5 Placeholder attribute' in document.createElement('input')) || ({}).toString.call(window.operamini) === '[object OperaMini]') { // 做点什么事}
value
值设置为 A detailed introduction to the HTML5 Placeholder attribute
的值来模拟显示 A detailed introduction to the HTML5 Placeholder attribute 的状态。再添加上事件处理程序,当文本输入框获取焦点时如果 value
的值为 A detailed introduction to the HTML5 Placeholder attribute 则清空文本输入框;当文本输入框失去焦点时如果 value
值为空则将 A detailed introduction to the HTML5 Placeholder attribute 的内容赋给它,同时当 A detailed introduction to the HTML5 Placeholder attribute 显示的时候应该给文本输入框添加一个 class="A detailed introduction to the HTML5 Placeholder attribute"
用来设置样式以区别是显示的 A detailed introduction to the HTML5 Placeholder attribute 和还是显示的普通 value:// 做点什么事$('input[A detailed introduction to the HTML5 Placeholder attribute]').on('focus', function() { var $this = $(this); if (this.value === $this.attr('A detailed introduction to the HTML5 Placeholder attribute') && $this.hasClass('A detailed introduction to the HTML5 Placeholder attribute')) { this.value = ''; $this.removeClass('A detailed introduction to the HTML5 Placeholder attribute'); }}).on('blur', function() { var $this = $(this); if (this.value === '') { $this.addClass('A detailed introduction to the HTML5 Placeholder attribute'); this.value = $this.attr('A detailed introduction to the HTML5 Placeholder attribute'); }});
value
值会显示为圆点之类的字符,呈现几个莫名其妙的圆点来作为 A detailed introduction to the HTML5 Placeholder attribute 提示恐怕不妥,因此需要特殊对待一下,将密码输入框拷贝一份出来然后修改其 type
属性为 'text' 来替代显示 A detailed introduction to the HTML5 Placeholder attribute,并把原本的密码输入框隐藏:$('input[A detailed introduction to the HTML5 Placeholder attribute]').on('blur', function() { var $this = $(this); var $replacement; if (this.value === '') { // 失去焦点时值为空则显示 A detailed introduction to the HTML5 Placeholder attribute if (this.type === 'password') { $replacement = $this.clone().attr('type', 'text'); $replacement.data('A detailed introduction to the HTML5 Placeholder attribute-password', $this); // 替代显示的文本输入框获取焦点时将它删掉,并且重新显示原来的密码输入框 $replacement.on('focus', function() { $(this).data('A detailed introduction to the HTML5 Placeholder attribute-password').show().focus(); $(this).remove(); }); $this.after($replacement).hide(); $this = $replacement; } $this.addClass('A detailed introduction to the HTML5 Placeholder attribute'); $this[0].value = $this.attr('A detailed introduction to the HTML5 Placeholder attribute'); }});
try { $replacement = $this.clone().prop('type', 'text'); // 使用 .prop() 方法在 IE 8 下会报错} catch(e) { $replacement = $('<input>').attr({ 'type': 'text', 'class': this.className // 还可以赋予 id, name 等属性 });}
value
值设为空,提交之后再恢复成显示 A detailed introduction to the HTML5 Placeholder attribute 的状态:$(document).on('submit', 'form', function() { var $input = $('.A detailed introduction to the HTML5 Placeholder attribute', this); $input.each(function() { this.value = ''; }); setTimeout(function() { $input.each(function() { this.value = $(this).attr('A detailed introduction to the HTML5 Placeholder attribute'); }); }, 10);});
beforeunload
事件来处理:$(window).on('beforeunload', function() { $('.A detailed introduction to the HTML5 Placeholder attribute').each(function() { this.value = ''; });});
input::-webkit-input-A detailed introduction to the HTML5 Placeholder attribute, textarea::-webkit-input-A detailed introduction to the HTML5 Placeholder attribute { color: #999;}input::-moz-A detailed introduction to the HTML5 Placeholder attribute, textarea::-moz-A detailed introduction to the HTML5 Placeholder attribute { color: #999; opacity: 1;}input:-ms-input-A detailed introduction to the HTML5 Placeholder attribute, textarea:-ms-input-A detailed introduction to the HTML5 Placeholder attribute { color: #999;}.A detailed introduction to the HTML5 Placeholder attribute { color: #999;}input:focus::-webkit-input-A detailed introduction to the HTML5 Placeholder attribute, textarea:focus::-webkit-input-A detailed introduction to the HTML5 Placeholder attribute { color: transparent;}input:focus::-moz-A detailed introduction to the HTML5 Placeholder attribute, textarea:focus::-moz-A detailed introduction to the HTML5 Placeholder attribute { color: transparent;}
The above is the detailed content of A detailed introduction to the HTML5 Placeholder attribute. For more information, please follow other related articles on the PHP Chinese website!