所以我知道IE10之前IE不支持HTML5 placeholder
属性,但我真的需要这个功能。文本输入占位符和跨浏览器支持
我想我能做到以下几点,但它似乎不工作(看起来像.focus
和.blur
方法不被解雇......
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
$(function() {
function isPlaceholderSupported() {
var input = document.createElement('input');
return ('placeholder' in input);
}
var placeholderSupport = isPlaceholderSupported();
if (placeholderSupport == false) {
$('#store_name').val('Store Name');
}
$('#store_name').focus(function() {
if (placeholderSupport == false) {
if ($(this).val() == 'Store Name') {
$(this).val('');
}
}
});
$('#store_name').blur(function() {
if (placeholderSupport == false) {
if ($(this).val() == '') {
$(this).val('Store Name');
}
}
});
});
</script>
<input type="text" name="store_name" id="store_name" placeholder="Store Name" />
我看不出任何破绽在这里的逻辑,但这恰恰是行不通的。有被报道在Chrome的开发工具没有错误,我还没有在IE9尝试过呢。
任何帮助将不胜感激!
有各种[现有'placeholder' polyfills(这个人是我写的这个方法)](https://github.com/jamesallardice/Placeholders.js),你可能要考虑使用。 –
我会看看,但我想避免使用太多不同的脚本引用和东西 – Ortund
如果你想写你自己的,你可能想考虑的事情,如你的当前版本会导致占位符文本是事实如果用户没有更改该值,则与表单一起提交。 –