0

有没有办法使用 HTML 条件标签,如<!--[if (IE 6)|(IE 7)]>如果访问者使用的是 IE6 或 IE7,我需要在 .js 文件中

谢谢

4

4 回答 4

3

我会用标记做这样的事情......

<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class=""> <!--<![endif]-->
<head>

然后在您的 JS 中,您可以检查适当的正文样式:

if (html tag has class lt-ie8) {
  // we're in ie7 and below land
}
于 2013-06-28T16:01:12.797 回答
1

您可以在标记中使用条件注释,并使用结果在 js 中创建条件变量。用这个开始你的文档:

<!DOCTYPE HTML>
<!--[if lte IE 7]><html lang="en" class="ieDetect ie7Detect"><![endif]-->
<!--[if IE 8]><html lang="en" class="ieDetect ie8Detect"><![endif]-->
<!--[if IE 9]><html lang="en" class="ieDetect ie9Detect"><![endif]-->
<!--[if !IE]>--><html lang="en"><!--<![endif]-->

我通常更喜欢为此使用 jQuery 并检查类:

var ieDetect = $('.ieDetect').length
,   ie7Detect = $('.ie7Detect').length
,   ie8Detect = $('.ie8Detect').length
,   ie9Detect = $('.ie9Detect').length
;

if (ieDetect){
  // do stuff with IE
}

如果您只想使用 JS,您可以使用类获取函数并保留上面的标记,或者您可以放弃通用的 ieDetect 类,只使用 ie7Detect(和 8,9)作为 html 标签上的 ID。

var ie7Detect = document.getElementById("ie7Detect").length
,   etc...
于 2013-06-28T16:04:31.130 回答
0

单独的 JS 文件是最好的选择。

但是,您可以使用 navigator.userAgent:

var userAgent = navigator.userAgent;

由此您可以确定用户用于查看您的页面的浏览器并使用 JS 中内置的 if 语句。

于 2013-06-28T15:10:50.397 回答
-1

用户navigator.userAgent检查用户拥有的浏览器。

推荐阅读:http ://api.jquery.com/jQuery.support/

于 2013-06-28T15:08:48.873 回答