Javascript检查IE8中的本机占位符支持 [英] Javascript check for native placeholder support in IE8
问题描述
tldr:
尽管没有对占位符属性的原生支持,为什么(在inputElemnt中''占位符')在IE8中等于true?不是(元素中的属性)检查本机支持的好方法吗? Javascript库Modernizer使用它。
tldr: Why does ('placeholder' in inputElemnt) equal true in IE8 despite no native support for the placeholder attribute? Isn't (attribute in element) a good way to check for native support? The Javascript library Modernizer use it.
长:
我有一个名为Defaultvalue的小型Jquery插件( http://unwrongest.com/projects/defaultvalue/ )。我有一个名为Placeholder的小Jquery插件( https://github.com/janjarfalk/jquery.placeholder.js )。它基本上是HTML5占位符属性的后备。
Long:
I have a small Jquery plugin called Defaultvalue ( http://unwrongest.com/projects/defaultvalue/ ). I have a small Jquery plugin called Placeholder ( https://github.com/janjarfalk/jquery.placeholder.js ). It's basically a fallback for the HTML5 placeholder attribute.
在最近的更新中,我添加了这三行代码。希望如果浏览器本身支持占位符属性,则不会运行Defaultvalue。
In a recent updated I added these three lines of code. Hoping that Defaultvalue wouldn't run if the browser had native support for the placeholder attribute.
if('placeholder' in this){
// this is an input-element
return false;
}
除IE8和IE7外,它似乎适用于大多数浏览器。由于某种原因,它在此找到了关键的'占位符',但我认为,IE7 / IE8中没有对占位符属性的任何支持。
It seems to work in most browsers except IE8 and IE7. For some reason it finds the key 'placeholder' in this, but there isn't, I think, any support for the placeholder attribute in IE7/IE8.
我的代码灵感来自Javascript库Modernizer中的此代码( http://www.modernizr.com/ )。
My code was inspired by this code in the Javascript library Modernizer ( http://www.modernizr.com/ ).
(function(props) {
for (var i = 0, len = props.length; i < len; i++) {
attrs[ props[i] ] = !!(props[i] in inputElem);
}
return attrs;
})('autocomplete autofocus list placeholder max min multiple pattern required step'.split(' '));
我缺少什么?
推荐答案
创建一个新的原始输入元素解决了我的问题。
Creating a new raw input element solved my problem.
var nativePlaceholderSupport = (function() {
var i = document.createElement('input');
return i.placeholder !== undefined;
})();
if(nativePlaceholderSupport){
return false;
}
var nativePlaceholderSupport = (function(){
var i = document.createElement('input');
return ('placeholder' in i);
})();
if(nativePlaceholderSupport){
return false;
}
谢谢RobG,你带我去了。
Thanks RobG, you led me to it.
这篇关于Javascript检查IE8中的本机占位符支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!