2012-11-01 2 views
0

이 방법을 사용하여 텍스트를 찾아서 바꾸고 왜 작동하지 않는지 확신하지 못합니까? console.log를 사용하면 바꿀 내용이 올바르게 표시되지만 최종 결과가 작동하지 않습니다.jQuery가 텍스트를 바꾸지 못합니다.

의견이 있으십니까?

감사합니다.

+0

하려면 string.replace 문자열을 반환 검색도 theContent의 HTML로 다시 지정하는 문자열에 추가 :이 당신이 참조하는 문자열 ...'theContent.html (theContent.html(). replace (/ Total : shipping : 'Total without shipping :')),' – Archer

+0

@diEcho 이것은 PHP가 아니며 정규 표현식을 인용 부호. – Barmar

답변

0

당신은

Live Demo

$(document).ready(function() { 
     var theContent = $(".transaction-results p").last(); 
     console.log(theContent.html()); 
     theContent.html(theContent.html().replace(/Total/, 'Total without shipping:')); 
    }); 
3

문자열이 대체되었지만 문자열을 요소의 html로 재 할당하지 않았습니다. return 사용

(친절 diEcho로 기여)
theContent.html(function(i,h){ 
    return h.replace(/Total:/, 'Total without shipping:'); 
}); 

JS Fiddle demo.

참고 :

+0

작동 데모 추가 – diEcho

+1

친절하게 감사드립니다! –

0
(function($) { 
    $(document).ready(function() { 
     var theContent = $(".transaction-results p").last(); 
     console.log(theContent.html()); 
     theContent.html(theContent.html().replace('Total:', 'Total without shipping:')); 
    }); 
})(jQuery); 

은 일반 문자열과 같은 /Total:/하지 'Total'을 한 이유는 무엇입니까?

@David Thomas의 솔루션이 작동합니다. 그것은 온 대체하지 않습니다 -

+0

그는 정규 표현식을 사용했기 때문에 문자열이 아닙니다. –

관련 문제