HTML5 占位符功能检测问题
我需要测试占位符支持。以下代码在所有现代浏览器以及 IE7、IE8、IE9 中都可以正常工作:
$.support.placeholder = (function () {
var i = document.createElement("input");
return "placeholder" in i;
}());
它可以工作,但 JSLint 抱怨 in
的使用:
意外的“进来”。与 undefined 进行比较,或者使用 hasOwnProperty 方法代替。
好吧,所以我将其重构为:
$.support.placeholder = (function () {
var i = document.createElement("input");
return i.hasOwnProperty("placeholder");
}());
现在,这通过了 JSLint,没有任何错误或警告,但它在 IE7 和 IE8 中用这个老栗子打破了:
对象不支持属性或方法“hasOwnProperty”
知道如何使 JSLint 以及 IE7 和 IE8 满意吗?
I need to test for placeholder support. The following works great in all modern browsers, as well as IE7, IE8, IE9:
$.support.placeholder = (function () {
var i = document.createElement("input");
return "placeholder" in i;
}());
It works, but JSLint complains about the use of in
:
Unexpected 'in'. Compare with undefined, or use the hasOwnProperty
method instead.
Fine, so I'll refactor it to this:
$.support.placeholder = (function () {
var i = document.createElement("input");
return i.hasOwnProperty("placeholder");
}());
Now this passes JSLint without any errors or warnings, but it breaks in IE7 and IE8 with this old chestnut:
Object doesn't support property or method 'hasOwnProperty'
Any idea how to make JSLint happy, as well as IE7 and IE8?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您还可以使用 JSLint 建议的其他解决方案:
这应该可以毫无问题地跨浏览器工作。
You could also use the other solution JSLint suggests:
This should work cross browser without problems.
我的回答是不要。不要让 JSLint 高兴。 JSLint 是 Crockford 认为 JavaScript 应该完成的方式。这是他个人的标准。如果您想要某种 JavaScript lint,请使用 JSHint。它是 JSLint 的分叉版本,完全可配置且没有疯狂的要求。从它的主页:
My answer would be don't. Don't make JSLint happy. JSLint is how Crockford views JavaScript should be done. It's his personal standard. If you want some kind of lint for JavaScript, use JSHint. It's a forked version of JSLint that is entirely configurable and without the crazy requirements. From it's homepage:
您可以通过
Object.prototype
获取该函数,然后在元素上调用
它。这使得该函数可用并且您可以以i.hasOwnProperty
时尚的方式调用它(即后面的this
值)调用时的场景是i
):You can fetch the function via
Object.prototype
, thencall
it on the element. This makes for the function being available and you being able to call it in ai.hasOwnProperty
-fashion way (i.e. thethis
value behind the scenes when calling it isi
):