开发者

Use JavaScript to add a Class to the <html> element respective to the browser

开发者 https://www.devze.com 2023-04-03 21:00 出处:网络
I kind of know how Modernizr works, by adding a Class to the <html>开发者_Python百科 element, I know thatjQuery Browser Detection is deprecated, is there a way to detect and add a class ?Detecti

I kind of know how Modernizr works, by adding a Class to the <html>开发者_Python百科 element, I know that jQuery Browser Detection is deprecated, is there a way to detect and add a class ?


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:

document.body.className = "xxxx";

or to add a class, you would use this:

document.body.className += " xxxxx";
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号