Html 未显示输入占位符
我不知道为什么我的占位符没有显示出来。这是代码Html 未显示输入占位符,html,Html,我不知道为什么我的占位符没有显示出来。这是代码 <label>Input here:</label><span><input placeholder="Enter Epoch Time" type="number" id='my-id-is-here'></span> <label>Input2 here:</label><span><input placeholder="10" type="
<label>Input here:</label><span><input placeholder="Enter Epoch Time" type="number" id='my-id-is-here'></span>
<label>Input2 here:</label><span><input placeholder="10" type="number" id='my-id2-is-here'></span>
在此处输入:
在此处输入2:
当我加载页面时,没有任何占位符,只有空白输入
谢谢。试试这个解决方案:
jQuery(function() {
jQuery.support.placeholder = false;
p = document.createElement('form');
if('placeholder' in p) jQuery.support.placeholder = true;
});
$(function() {
if(!$.support.placeholder) {
var active = document.activeElement;
$('input[type="text"], textarea').focus(function () {
if ($(this).attr('placeholder') != '' && $(this).val() == $(this).attr('placeholder')) {
$(this).val('').removeClass('hasPlaceholder');
}
}).blur(function () {
if ($(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
$(this).val($(this).attr('placeholder')).addClass('hasPlaceholder');
}
});
$('input[type="text"], textarea').blur();
$(active).focus();
$('form:eq(0)').submit(function () {
$('input[type="text"].hasPlaceholder, textarea.hasPlaceholder').val('');
});
}
});
这个jQuery插件应该可以做到这一点 支持IE6这是一个可靠的修复,我已经在IE5上测试过了:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Placeholder Fix</title>
</head>
<body>
<label>Input here:</label><span><input placeholder="Enter Epoch Time" type="number" id='my-id-is-here'></span>
<label>Input2 here:</label><span><input placeholder="10" type="number" id='my-id2-is-here'></span>
<style type="text/css">
.placeholder {
color: red;
font-weight: normal;
}
</style>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.1.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/modernizr/modernizr-2.7.1.js"></script>
<script>
if (!Modernizr.input.placeholder) {
alert('in')
$('[placeholder]').focus(function () {
var input = $(this);
if (input.val() == input.attr('placeholder')) {
input.val('');
input.removeClass('placeholder');
}
}).blur(function () {
var input = $(this);
if (input.val() == '' || input.val() == input.attr('placeholder')) {
input.addClass('placeholder');
input.val(input.attr('placeholder'));
}
}).blur();
$('[placeholder]').parents('form').submit(function () {
$(this).find('[placeholder]').each(function () {
var input = $(this);
if (input.val() == input.attr('placeholder')) {
input.val('');
}
})
});
}
</script>
</body>
</html>
占位符修复
在此处输入:
在此处输入2:
.占位符{
颜色:红色;
字体大小:正常;
}
if(!modernizer.input.placeholder){
警报('in')
$(“[占位符]”)。焦点(函数(){
var输入=$(此);
if(input.val()==input.attr('placeholder')){
input.val(“”);
input.removeClass('placeholder');
}
}).blur(函数(){
var输入=$(此);
if(input.val()=''| input.val()==input.attr('placeholder')){
input.addClass(“占位符”);
val(input.attr(‘占位符’);
}
}).blur();
$(“[占位符]”)。父项('form')。提交(函数(){
$(this).find(“[占位符]”).each(函数(){
var输入=$(此);
if(input.val()==input.attr('placeholder')){
input.val(“”);
}
})
});
}
当页面加载到不处理HTML5属性(如占位符
)的浏览器中时,占位符文本将变为红色,以证明Modernizer仅在浏览器不处理HTML5占位符
属性时才会添加/处理该属性,很明显,您可以将文本从红色更改为标准的\999
或您喜欢的任何颜色
享受吧 似乎是。您在哪个浏览器上测试?IE8?您的输入标记写为自动关闭,末尾没有前斜杠。可能无法解决问题,但该部分是错误的。不需要
/
。这可能是你的浏览器或其他地方的一个愚蠢的打字错误。还有,你为什么要混合使用“
和”
?什么浏览器:进来吧:我看不出你的代码有任何问题。确保你已经声明了html5 DOCTYPE并使用了更新的浏览器。请记住,此脚本使用jQuery覆盖所有浏览器,而不仅仅是那些没有实现自己的默认处理的浏览器(占位符)HTML5属性(例如,小于Internet Explorer 10)。