HTML5 占位符功能检测问题

发布于 2024-12-17 10:40:10 字数 681 浏览 4 评论 0原文

我需要测试占位符支持。以下代码在所有现代浏览器以及 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

弃爱 2024-12-24 10:40:10

您还可以使用 JSLint 建议的其他解决方案:

return typeof i.placeholder !== 'undefined';

这应该可以毫无问题地跨浏览器工作。

You could also use the other solution JSLint suggests:

return typeof i.placeholder !== 'undefined';

This should work cross browser without problems.

帅气尐潴 2024-12-24 10:40:10

我的回答是不要。不要让 JSLint 高兴。 JSLint 是 Crockford 认为 JavaScript 应该完成的方式。这是他个人的标准。如果您想要某种 JavaScript lint,请使用 JSHint。它是 JSLint 的分叉版本,完全可配置且没有疯狂的要求。从它的主页:

JSHint 是 JSLint 的一个分支,JSLint 是由 Douglas 编写和维护的工具
克罗克福德。

该项目最初是为了让更多人
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:

JSHint is a fork of JSLint, the tool written and maintained by Douglas
Crockford.

The project originally started as an effort to make a more
configurable version of JSLint—the one that doesn't enforce one
particular coding style on its users—but then transformed into a
separate static analysis tool with its own goals and ideals.

永不分离 2024-12-24 10:40:10

您可以通过Object.prototype获取该函数,然后在元素上调用它。这使得该函数可用并且您可以以i.hasOwnProperty时尚的方式调用它(即后面的this值)调用时的场景是i):

Object.prototype.hasOwnProperty.call(i, "placeholder");

You can fetch the function via Object.prototype, then call it on the element. This makes for the function being available and you being able to call it in a i.hasOwnProperty-fashion way (i.e. the this value behind the scenes when calling it is i):

Object.prototype.hasOwnProperty.call(i, "placeholder");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文