Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
重新编写一个Javascript函数,用于选择活动导航选项卡以在IE8中工作_Javascript_Jquery_Html_Css_Internet Explorer 8 - Fatal编程技术网

重新编写一个Javascript函数,用于选择活动导航选项卡以在IE8中工作

重新编写一个Javascript函数,用于选择活动导航选项卡以在IE8中工作,javascript,jquery,html,css,internet-explorer-8,Javascript,Jquery,Html,Css,Internet Explorer 8,我在最近的一个项目中继承了一些代码,该项目使用一些CSS3选择器向一些顶级选项卡导航添加类。由于在后端实现的方式,JS正在搜索window.location.pathname以匹配字符串,然后在另一个函数中使用该字符串在DOM中添加类名。第二个函数使用第n个子(x),其中x是一个循环,对应于路径名和li中相应的匹配字符串。但是,正如您所知,IE8不支持第n个子项 我打算在IE8中使用模仿第n个child的风格,但我不清楚如何编写函数来将其实际添加到现有代码中 以下是我引用的JS代码: var t

我在最近的一个项目中继承了一些代码,该项目使用一些CSS3选择器向一些顶级选项卡导航添加类。由于在后端实现的方式,JS正在搜索window.location.pathname以匹配字符串,然后在另一个函数中使用该字符串在DOM中添加类名。第二个函数使用第n个子(x),其中x是一个循环,对应于路径名和li中相应的匹配字符串。但是,正如您所知,IE8不支持第n个子项

我打算在IE8中使用模仿第n个child的风格,但我不清楚如何编写函数来将其实际添加到现有代码中

以下是我引用的JS代码:

var tabName = 'Product', tabType = window.location.pathname;
    if(tabType.indexOf('Product')>-1) tabName = 'Product'; 
    else if(tabType.indexOf('Business')>-1) tabName = 'Business'; 
    else if(tabType.indexOf('Support')>-1) tabName = 'Support';
    else if(tabType.indexOf('Article')>-1) tabName = 'Article';

$('.category-tabs li').removeClass('active');
for( var tn = 1; tn < $('.category-tabs li').length+1; tn ++ ){
    if($('.category-tabs li:nth-child(' + tn + ') a').html().indexOf(tabName)>-1){
        $('.category-tabs li:nth-child(' + tn + ')').addClass('active');
    } 
}
var tabName='Product',tabType=window.location.pathname;
if(tabType.indexOf('Product')>-1)tabName='Product';
else if(tabType.indexOf('Business')>-1)tabName='Business';
else if(tabType.indexOf('Support')>-1)tabName='Support';
else if(tabType.indexOf('Article')>-1)tabName='Article';
$('.category选项卡li').removeClass('active');
对于(变量tn=1;tn<$('.category tabs li')。长度+1;tn++){
if($('.category tabs li:n子('+tn+').html().indexOf(tabName)>-1){
$('.category选项卡li:n子项('+tn+')).addClass('active');
} 
}
我想添加一个使用这个循环的函数,但是也将它设置为“li”,这样我就可以使用li:first child+li方法来模拟第n个child,但是我真的需要一些帮助来实现这一点


谢谢

>因为你已经在使用jQuery考虑用它解决问题了。只需使用.eq方法即可访问列表中的特定项。例如:

var tabName = 'Product', tabType = window.location.pathname;
    if(tabType.indexOf('Product')>-1) tabName = 'Product'; 
    else if(tabType.indexOf('Business')>-1) tabName = 'Business'; 
    else if(tabType.indexOf('Support')>-1) tabName = 'Support';
    else if(tabType.indexOf('Article')>-1) tabName = 'Article';

var liTags = $('.category-tabs li');
liTags.removeClass('active');
for( var tn = 1; tn < liTags.length+1; tn ++ ){
    var li = liTags.eq(i);
    if(li.find('a').html().indexOf(tabName)>-1){
        li.addClass('active');
    } 
}
var tabName='Product',tabType=window.location.pathname;
if(tabType.indexOf('Product')>-1)tabName='Product';
else if(tabType.indexOf('Business')>-1)tabName='Business';
else if(tabType.indexOf('Support')>-1)tabName='Support';
else if(tabType.indexOf('Article')>-1)tabName='Article';
var liTags=$('.category tabs li');
liTags.removeClass(“活动”);
对于(变量tn=1;tn-1){
li.addClass(“活动”);
} 
}

并尝试缓存jQuery选择。它更加清晰易读。

为什么不直接使用与选项卡名称相对应的CSS类名,而不是通过索引访问它们呢?试试selectivizr=>Diodeus,这是动态完成的。应用程序使用ATG,因此选项卡的名称可以完全自定义,甚至可以自定义到标记。我认为,因为他已经在使用jQuery,他可能会使用它来解决问题,并避免使用css花式选择器。Krasimir,这适用于除第一个以外的每个导航选项卡(一旦我将整数“I”更改为“tn”)。奇怪的是,我在这行中得到了一个未定义的错误:if(li.find('a').html().indexOf(tabName)>-1{有任何线索说明我为什么会得到这个结果,以及为什么它在第一个导航选项卡上不起作用吗?我猜这是因为循环从1开始。如果将其更改为for(var tn=0;tn