使用 JavaScript 将类添加到中与浏览器相关的元素
我知道 Modernizr 是如何工作的,通过向 元素添加一个类,我知道 jQuery 浏览器检测已被弃用,有没有办法检测并添加一个类?
I kind of know how Modernizr works, by adding a Class to the <html>
element, I know that jQuery Browser Detection is deprecated, is there a way to detect and add a class ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检测浏览器并不困难(尽管通常不推荐并且有时不准确)。如果您熟悉 Modernizr,那么您应该知道功能检测是一种更易于维护且更准确的机制,用于识别您应该在主机浏览器中使用哪些行为。对于实际的浏览器检测(再次强调,不推荐),我建议阅读这篇 Mozilla 文章: https://developer .mozilla.org/En/Browser_Detection_and_Cross_Browser_Support。
对于设置类来说,这要容易得多。要在主体对象上设置类,您只需执行以下操作:
或者添加类,您将使用以下命令:
Detecting a browser is not difficult (though generally not recommended and occasionally inaccurate). If you're familiar with modernizr, then you should know that feature detection is much more maintainable and accurate mechanism for identifying which behavior you should use in the host browser. For actual browser detection (again, not recommended), I'd suggest reading this Mozilla article: https://developer.mozilla.org/En/Browser_Detection_and_Cross_Browser_Support.
For setting a class, that's much easier. To set a class on the body object, you would just do this:
or to add a class, you would use this: