2012-10-07 2 views
3

나는 eBay 용 템플릿을 코딩하고 있습니다. 그러나 eBay는 .replace를 허용하지 않습니다. 아래 코드는 롤오버 탭 섹션입니다. 사용자가 탭 (a) 위로 마우스를 가져 가면 해당 div div (a)가 표시됩니다.자바 스크립트. 대체품 대체

.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". 교체 ('REPLACETHIS', 'WITHTHIS'); – Doug

관련 문제