我需要测试占位符支持。以下内容适用于所有现代浏览器以及 IE7、IE8、IE9:
$.support.placeholder = (function () {
var i = document.createElement("input");
return "placeholder" in i;
}());
有效,但 JSLint 提示 in
的使用:
Unexpected 'in'. Compare with undefined, or use the hasOwnProperty method instead.
好吧,我将把它重构为:
$.support.placeholder = (function () {
var i = document.createElement("input");
return i.hasOwnProperty("placeholder");
}());
现在这通过了 JSLint,没有任何错误或警告,但它在 IE7 和 IE8 中用这个老栗子打破了:
Object doesn't support property or method 'hasOwnProperty'
知道如何使 JSLint 以及 IE7 和 IE8 快乐吗?
最佳答案
您还可以使用 JSLint 建议的其他解决方案:
return typeof i.placeholder !== 'undefined';
这应该可以毫无问题地跨浏览器工作。
关于javascript - HTML5 占位符功能检测问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8245093/