添加用户代理的数据属性

Avatar of Chris Coyier
Chris Coyier
var b = document.documentElement;
b.className = b.className.replace('no-js', 'js');
b.setAttribute("data-useragent",  navigator.userAgent);
b.setAttribute("data-platform", navigator.platform );

这将导致数据属性被添加到html元素,例如

<html 
	data-useragent="Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C)"
	data-platform="Win32">
	...

这使您能够使用CSS定位特定平台上的非常特定的浏览器

html[data-useragent*="Chrome/13.0"][data-platform="Win32"] {
  ...
}

这来自Rogie King的一篇帖子,该帖子现已下线。