jquery - Re-write a Javascript function that selects active navigation tabs to work in IE8 -
i have inherited code in recent project makes use of css3 selectors add classes top level tab navigation. due way has been implemented on back-end, js searching window.location.pathname match string, , using in function add classname in dom. second function using nth-child(x) x loop corresponds appropriate matching string in pathname , li. however, know, nth-child not supported in ie8.
i going use this style of emulating nth-child in ie8, unclear how write function add existing code.
here js code referencing:
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 add function uses loop, sets equal "li" can use li:first-child + li method of emulating nth-child, use in making work.
thanks!
because using jquery consider solving problem it. use .eq method access specific item in list. 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 cache jquery selections. it's cleaner , readable.
Comments
Post a Comment