Javascript 所有IE上的Html5占位符问题
我似乎对HTML5占位符有一个奇怪的问题。 我用一个下拉菜单来显示/隐藏div,在一个div中我有几个文本字段 当我从下拉列表中选择一个选项来显示div时,div将显示,但占位符不在文本字段中 任何帮助都将不胜感激Javascript 所有IE上的Html5占位符问题,javascript,jquery,html,placeholder,Javascript,Jquery,Html,Placeholder,我似乎对HTML5占位符有一个奇怪的问题。 我用一个下拉菜单来显示/隐藏div,在一个div中我有几个文本字段 当我从下拉列表中选择一个选项来显示div时,div将显示,但占位符不在文本字段中 任何帮助都将不胜感激 $(文档).ready(函数(){ $(“#customertype”).change(函数(){ if($(“#customertype option[value='new']]).attr('selected')){ $('#newcustomer').show(); } i
$(文档).ready(函数(){
$(“#customertype”).change(函数(){
if($(“#customertype option[value='new']]).attr('selected')){
$('#newcustomer').show();
}
if($(“#customertype option[value='existingcustomer']])attr('selected')){
$('#newcustomer').hide();
$(“#现有客户”).show();
}
});
});
IE不支持占位符,至少从9到9
使用以下代码:
//IE placeholder;
$(function (){
if (/MSIE 9|MSIE 8|MSIE 7|MSIE 6/g.test(navigator.userAgent)) {
function resetPlaceholder() {
if ($(this).val() === '') {
$(this).val($(this).attr('placeholder'))
.attr('data-placeholder', true)
.addClass('ie-placeholder');
if ($(this).is(':password')) {
var field = $('<input />');
$.each(this.attributes, function (i, attr) {
if (attr.name !== 'type') {
field.attr(attr.name, attr.value);
}
});
field.attr({
'type': 'text',
'data-input-password': true,
'value': $(this).val()
});
$(this).replaceWith(field);
}
}
}
$('[placeholder]').each(function () {
//ie user refresh don't reset input values workaround
if ($(this).attr('placeholder') !== '' && $(this).attr('placeholder') === $(this).val()){
$(this).val('');
}
resetPlaceholder.call(this);
});
$(document).on('focus', '[placeholder]', function () {
if ($(this).attr('data-placeholder')) {
$(this).val('').removeAttr('data-placeholder').removeClass('ie-placeholder');
}
}).on('blur', '[placeholder]', function () { resetPlaceholder.call(this); });
$(document).on('focus', '[data-input-password]', function () {
var field = $('<input />');
$.each(this.attributes, function (i, attr) {
if (['type','data-placeholder','data-input-password','value'].indexOf(attr.name) === -1) {
field.attr(attr.name, attr.value);
}
});
field.attr('type', 'password').on('focus', function () { this.select(); });
$(this).replaceWith(field);
field.trigger('focus');
});
}
});
//IE占位符;
$(函数(){
如果(/MSIE 9 | MSIE 8 | MSIE 7 | MSIE 6/g.test(navigator.userAgent)){
函数resetPlaceholder(){
if($(this.val()=''){
$(this.val($(this.attr('placeholder'))
.attr('data-placeholder',true)
.addClass(“ie占位符”);
如果($(this).is(':password')){
变量字段=$('');
$.each(this.attributes,function(i,attr){
如果(attr.name!=='type'){
field.attr(attr.name,attr.value);
}
});
field.attr({
“类型”:“文本”,
“数据输入密码”:true,
'value':$(this.val()
});
$(此).replaceWith(字段);
}
}
}
$(“[占位符]”)。每个(函数(){
//ie用户刷新不重置输入值解决方法
if($(this.attr('placeholder')!=''&$(this.attr('placeholder')===$(this.val()){
$(this.val(“”);
}
重置占位符。调用(此);
});
$(文档).on('focus','[placeholder]',函数(){
if($(this.attr('data-placeholder')){
$(this.val(“”).removeAttr('data-placeholder').removeClass('ie-placeholder');
}
}).on('blur','[placeholder]',函数(){resetPlaceholder.call(this);});
$(文档).on('focus','[data input password]',函数(){
变量字段=$('');
$.each(this.attributes,function(i,attr){
if(['type'、'data-placeholder'、'data-input-password'、'value'].indexOf(attr.name)=-1){
field.attr(attr.name,attr.value);
}
});
field.attr('type','password')。on('focus',function(){this.select();});
$(此).replaceWith(字段);
字段。触发器(“焦点”);
});
}
});
hey感谢您的回复,但是当值添加到字段时,它不会验证我的来源。您可以使用required属性验证表单,如下所示:
//IE placeholder;
$(function (){
if (/MSIE 9|MSIE 8|MSIE 7|MSIE 6/g.test(navigator.userAgent)) {
function resetPlaceholder() {
if ($(this).val() === '') {
$(this).val($(this).attr('placeholder'))
.attr('data-placeholder', true)
.addClass('ie-placeholder');
if ($(this).is(':password')) {
var field = $('<input />');
$.each(this.attributes, function (i, attr) {
if (attr.name !== 'type') {
field.attr(attr.name, attr.value);
}
});
field.attr({
'type': 'text',
'data-input-password': true,
'value': $(this).val()
});
$(this).replaceWith(field);
}
}
}
$('[placeholder]').each(function () {
//ie user refresh don't reset input values workaround
if ($(this).attr('placeholder') !== '' && $(this).attr('placeholder') === $(this).val()){
$(this).val('');
}
resetPlaceholder.call(this);
});
$(document).on('focus', '[placeholder]', function () {
if ($(this).attr('data-placeholder')) {
$(this).val('').removeAttr('data-placeholder').removeClass('ie-placeholder');
}
}).on('blur', '[placeholder]', function () { resetPlaceholder.call(this); });
$(document).on('focus', '[data-input-password]', function () {
var field = $('<input />');
$.each(this.attributes, function (i, attr) {
if (['type','data-placeholder','data-input-password','value'].indexOf(attr.name) === -1) {
field.attr(attr.name, attr.value);
}
});
field.attr('type', 'password').on('focus', function () { this.select(); });
$(this).replaceWith(field);
field.trigger('focus');
});
}
});