Javascript 检查 IE8 中的原生占位符支持

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5536236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 17:32:54  来源:igfitidea点击:

Javascript check for native placeholder support in IE8

javascriptplaceholder

提问by janjarfalk

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.

tldr:尽管没有对占位符属性的本机支持,为什么在 IE8 中 ('placeholder' in inputElemnt) 等于 true?(元素中的属性)不是检查本机支持的好方法吗?Javascript 库 Modernizer 使用它。

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 ( http://unwrongest.com/projects/defaultvalue/)的小型 Jquery 插件。我有一个名为 Placeholder ( https://github.com/janjarfalk/jquery.placeholder.js)的小型 Jquery 插件。它基本上是 HTML5 占位符属性的后备。

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.

在最近的更新中,我添加了这三行代码。希望如果浏览器具有对占位符属性的本机支持,则 Defaultvalue 不会运行。

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.

它似乎适用于除 IE8 和 IE7 之外的大多数浏览器。出于某种原因,它在这里找到了关键的“占位符”,但我认为在 IE7/IE8 中不支持占位符属性。

My code was inspired by this code in the Javascript library Modernizer ( http://www.modernizr.com/).

我的代码的灵感来自 Javascript 库 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?

我错过了什么?

采纳答案by janjarfalk

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;
}
var nativePlaceholderSupport = (function(){
    var i = document.createElement('input');
    return ('placeholder' in i);
})();

if(nativePlaceholderSupport){
    return false;
}

Thanks RobG, you led me to it.

谢谢 RobG,你让我明白了。

回答by ?ime Vidas

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

它没有。它在 IE9 和 IE8 中都等于 false。

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

现场演示:http: //jsfiddle.net/JVSgx/