tldr:为什么 ('placeholder' in inputElemnt) 在 IE8 中等于 true,尽管没有对 placeholder 属性的本机支持? (元素中的属性) 不是检查本机支持的好方法吗? Javascript 库 Modernizer 使用它。
Long:
I have a small Jquery plugin called Defaultvalue ( http://unwrongest.com/projects/defaultvalue/ http://unwrongest.com/projects/defaultvalue/ ). I have a small Jquery plugin called Placeholder ( https://github.com/janjarfalk/jquery.placeholder.js https://github.com/janjarfalk/jquery.placeholder.js ). It's basically a fallback for the HTML5 placeholder attribute.
在最近的更新中,我添加了这三行代码。希望如果浏览器对占位符属性具有本机支持,则 Defaultvalue 不会运行。
if('placeholder' in this){
// this is an input-element
return false;
}
它似乎适用于除 IE8 和 IE7 之外的大多数浏览器。由于某种原因,它在其中找到了关键的“占位符”,但我认为 IE7/IE8 中不支持占位符属性。
我的代码受到 Javascript 库 Modernizer 中这段代码的启发(http://www.modernizr.com/ 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(' '));
我缺少什么?
创建一个新的原始输入元素解决了我的问题。
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,你引导我做到了这一点。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)