2012-10-07 185 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