2012-10-07 79 views
3

我正在为eBay编码模板。但是,eBay不允许.replace。下面的代码用于翻转选项卡部分。当用户将鼠标悬停在选项卡(a)上时,相应的div div(a)将变为可见。Javascript。替代方案

是否有解决方法让代码在不使用.replace的情况下工作?

var divs = new Array(); 
divs.push("contentPayment"); 
divs.push("contentShipping"); 
divs.push("contentWarranty"); 
divs.push("contentContact"); 
var navs = new Array(); 
navs.push("nav1"); 
navs.push("nav2"); 
navs.push("nav3"); 
navs.push("nav4"); 
/////////////////////////////////////// 


function hasClass(element, cls) { 
    return (' ' + element.className + ' ').indexOf(' ' + cls + ' ') > -1; 
} 
/////////////////////////////////////////////////////////////////////// 


function toggleDisplay(id) { 
    for (var i = 0; i < divs.length; i++) { 
     var item = document.getElementById(divs[i]); 
     item.style.display = 'none'; 
    } 
    var target = document.getElementById(id); 
    target.style.display = 'block'; 
    /////////////////////////////////////////////////////////////////////////////////////////////////////// 
    ////////////////////////////PAYMENT IS HOVERED//////////////////////////////////////////////////////// 
    if (id == "contentPayment") { 
     var CurrentTab = document.getElementById("nav1"); 
     var AlreadyActive = hasClass(CurrentTab, "tabActive"); 
     if (AlreadyActive === false) { 
      for (var i = 0; i < navs.length; i++) { 
       var otherTabs = document.getElementById(navs[i]); 
       otherTabs.className = otherTabs.className.replace(' tabActive', ''); 
      } 
      CurrentTab.className += " " + "tabActive"; 
     } 
    } 

    ///////////////////////////////////////////////////////////////////////////////////////////// 
    ////////////////////////////Shipping IS HOVERED//////////////////////////////////////////////////////// 
    if (id == "contentShipping") { 
     var CurrentTab = document.getElementById("nav2"); 
     var AlreadyActive = hasClass(CurrentTab, "tabActive"); 
     if (AlreadyActive === false) { 
      for (var i = 0; i < navs.length; i++) { 
       var otherTabs = document.getElementById(navs[i]); 
       otherTabs.className = otherTabs.className.replace(' tabActive', ''); 
      } 
      CurrentTab.className += " " + "tabActive"; 
     } 
    } 

    /////////////////////////////////////////////////////////////////////////////////////////////////////// 
    ////////////////////////////Warranty IS HOVERED//////////////////////////////////////////////////////// 
    if (id == "contentWarranty") { 
     var CurrentTab = document.getElementById("nav3"); 
     var AlreadyActive = hasClass(CurrentTab, "tabActive"); 
     if (AlreadyActive === false) { 
      for (var i = 0; i < navs.length; i++) { 
       var otherTabs = document.getElementById(navs[i]); 
       otherTabs.className = otherTabs.className.replace(' tabActive', ''); 
      } 
      CurrentTab.className += " " + "tabActive"; 
     } 
    } 

    /////////////////////////////////////////////////////////////////////////////////////////////////////// 
    ////////////////////////////Contact IS HOVERED//////////////////////////////////////////////////////// 
    if (id == "contentContact") { 
     var CurrentTab = document.getElementById("nav4"); 
     var AlreadyActive = hasClass(CurrentTab, "tabActive"); 
     if (AlreadyActive === false) { 
      for (var i = 0; i < navs.length; i++) { 
       var otherTabs = document.getElementById(navs[i]); 
       otherTabs.className = otherTabs.className.replace(' tabActive', ''); 
      } 
      CurrentTab.className += " " + "tabActive"; 
     } 
    } 
} 
+0

我不知道它是否可以在代码中使用'String.prototype.rpl = String.prototype.replace;'然后使用'foo.rpl(/ x /,“y”)''。 – Pointy

+0

为什么不使用数组文字,例如'var arr = ['foo','bar','baz'];'? –

回答

1

你可以试试这个作为replace功能

String.prototype.fakeReplace=function(str, newstr) { 
    return this.split(str).join(newstr); 
}; 

var str="Welcome javascript"; 
str=str.fakeReplace('javascript', ''); 
alert(str); // Welcome 

DEMO的替代。

+0

是的,除了它创建一个中间数组,并且使用对'.join()'的调用,这是已知不太高效的。 – Chris

+0

@ Abody97,你可以发布一个更好的/有效的解决方案,我想知道是否有另一种方式比这更好,所以我可以学习。 –

+0

查看我刚刚发布的答案。 – Chris

0

对于一个更有效,但稍长的方法,使用此:

String.prototype.myReplace = function(pattern, nw) { 
    var curidx = 0, len = this.length, patlen = pattern.length, res = ""; 
    while(curidx < len) { 
     var nwidx = this.indexOf(pattern, curidx); 
     console.log(nwidx); 
     if(nwidx == -1) { 
      break; 
     } 
     res = res + this.substr(curidx, nwidx - curidx); 
     res = res + nw; 
     curidx = nwidx + patlen; 
    } 
    return res; 
}; 
alert("Glee is awesome".myReplace("awesome", "very very very awesome")); 

看到它在行动:little link

希望有帮助!

+0

对我而言没有效用:“http://www.longstring.com/imagename-REPLACETHIS.jpg”.Replace('-REPLACETHIS','WITHTHIS'); – Doug