如何测试浏览器是否支持原生占位符属性? [英] How to test if the browser supports the native placeholder attribute?

查看:97
本文介绍了如何测试浏览器是否支持原生占位符属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图为我的一个站点编写一个简单的占位符jQuery插件,但当然我只想在不支持原生占位符属性的情况下触发该函数...

如何使用jQuery来测试placeholder属性的原生支持? >您可以添加到 $。support 很容易插入到你写的Javascript的顶部:

  jQuery.support.placeholder =(function( ){
var i = document.createElement('input');
返回i中的'占位符';
})();

您可以使用 $。support.placeholder jQuery.support.placeholder 代码中的任何位置。



NB此代码由 diveintohtml5 ,链接由上面的hellvinz提供。


I'm trying to write a simple placeholder jQuery plugin for a site of mine but of course I only want to fire the function if the native placeholder attribute isn't supported…

How can I use jQuery to test for native support of the placeholder attribute?

解决方案

You can add to $.support quite easily by inserting this at the top of the Javascript you've written:

jQuery.support.placeholder = (function(){
    var i = document.createElement('input');
    return 'placeholder' in i;
})();

You can then use either $.support.placeholder or jQuery.support.placeholder anywhere in your code.

NB This code adapted from diveintohtml5, the link provided by hellvinz above.

这篇关于如何测试浏览器是否支持原生占位符属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆