简体   繁体   English

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

[英]Re-write a Javascript function that selects active navigation tabs to work in IE8

I have inherited some code in a recent project that makes use of some CSS3 selectors to add classes to some top level tab navigation. 我在最近的项目中继承了一些代码,该项目利用一些CSS3选择器将类添加到某些顶级选项卡导航中。 Due to the way this has been implemented on the back-end, the JS is searching the window.location.pathname to match a string, and then using that in another function to add the classname in the DOM. 由于已在后端实现此方法,因此JS正在搜索window.location.pathname以匹配字符串,然后在另一个函数中使用该名称在DOM中添加类名。 This second function is using nth-child(x) where x is a loop that corresponds to the appropriate matching string in the pathname and li. 第二个函数使用nth-child(x),其中x是一个循环,该循环对应于路径名和li中的适当匹配字符串。 However, as you know, nth-child is not supported in IE8. 但是,如您所知,IE8不支持nth-child。

I was going to use this style of emulating nth-child in IE8, but I am unclear how to write the function to actually add this into the existing code. 我本来打算在IE8中使用这种模拟nth-child的风格,但是我不清楚如何编写函数以将其实际添加到现有代码中。

Here is the JS code I am referencing: 这是我引用的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');
    } 
}

I would like to add a function that uses this loop, but then also sets it equal to a "li" so that I can use the li:first-child + li method of emulating nth-child, but I could really use some help in making this work. 我想添加一个使用此循环的函数,然后将其设置为等于“ li”,以便我可以使用模仿nth-child的li:first-child + li方法,但是我确实可以使用一些帮助使这项工作。

Thanks! 谢谢!

Because you are already using jQuery consider solving the problem with it. 因为您已经在使用jQuery,请考虑解决它的问题。 Just use .eq method to get an access to the specific item in the list. 只需使用.eq方法即可访问列表中的特定项目。 For example: 例如:

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');
    } 
}

And try to cache the jQuery selections. 并尝试缓存jQuery选择。 It's just much cleaner and readable. 它更加清晰易读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM