Javascript check for native placeholder support in IE8

核能气质少年 提交于 2019-12-23 06:57:24

问题


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.

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.

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;
}

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.

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(' '));

What am I missing?


回答1:


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;
}

Thanks RobG, you led me to it.




回答2:


It doesn't. It equals to false in both IE9 and IE8.

Live demo: http://jsfiddle.net/JVSgx/



来源:https://stackoverflow.com/questions/5536236/javascript-check-for-native-placeholder-support-in-ie8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!