-2

是否可以使用 jQuery 以类似于 CSS 的方式链接类和 ID?例如,如果我希望针对不同的浏览器,我会将以下内容应用于我的 HTML 标记:

    <!--[if lt IE 7]>  <html class="ie ie6 lte9 lte8 lte7"> <![endif]-->

        <!--[if IE 7]>     <html class="ie ie7 lte9 lte8 lte7"> <![endif]-->

        <!--[if IE 8]>     <html class="ie ie8 lte9 lte8"> <![endif]-->

        <!--[if IE 9]>     <html class="ie ie9 lte9"> <![endif]-->

        <!--[if gt IE 9]>  <html> <![endif]-->

        <!--[if !IE]><!--> <html class="notIE">             <!--<![endif]-->

这将允许我专门使用以下方法来定位 ie7 中的元素:

.ie7 #div{
 float: left
}

我希望在我的网站上遵循优雅的降级模式,并且我知道我希望使用的某个 jQuery 插件在 IE7 或 IE8 中无法正常工作(例如,它会使样式偏离位置)因此我希望阻止 jQuery在 IE7 和 IE8 中触发的插件。我该怎么做?

4

2 回答 2

1

你的选择器应该可以工作,所以值得用简单的东西来测试它们

$(".lte9 #head").css('color','red');

如果这不起作用,那么您的选择器是错误的。

于 2013-02-12T17:14:19.933 回答
-1

是的,你可以在 jQuery 中做到这一点。事实上,它与您在 CSS 中看到的符号几乎相同。例如,让我们看看下面的这个函数,它旨在在悬停时稍微旋转图像。我希望将此函数应用于 id 为“head”的图像(Rotate 是一个 jQuery 插件,不是基本库的一部分)。假设我们希望以 ie9 为目标。我们将简单地使用下面的符号:

$(document).ready(function()
{   
$(".ie9 #head").hover(function(){
    $(".ie9 #head").rotate(-10);
    },

    function() {
    $(".ie9 #head").rotate(0);
    }
  );
});

请注意标头 id 是如何以 .ie9 类为前缀的。这与您在 CSS 中使用的方法完全相同。

于 2013-02-12T17:00:43.553 回答