2011-01-18 7 views
0

jquery와 javascript를 사용하여 DOM (메모리의 웹 페이지)에있는 모든 단어를 바꿔야합니다. 이것으로자바 스크립트 모든 단어 바꾸기

<html> 
<head> 
    Hi to all 
</head> 
<body> 
    Hi to all 
</body> 
</html> 

:

<html> 
    <head> 
     Bye to all 
    </head> 
    <body> 
     Bye to all 
    </body> 
</html> 

을하지만 태그 (거기에 값만)

한 시도는 수정하지 않고 :

jQuery.fn.replaceEachOne = function (objective, rep) { 
    return this.each(function(){ 
       $(this).html($(this).html().replace(new RegExp('(\\s'+objective+'\\s(?![[\\w\\s?&.\\/;#~%"=-]*>]))', "ig"), rep)); 
      } 
     ); 
} 
을 나는이 변환 할 필요가

하지만 태그 값을 바꾸면 계속해서 페이지를 나눕니다.

도움말 !!!

+0

@itchy : 점 2에 관해서는, jQuery를 질문에 명시되어있다. – user113716

+0

@patrick dw : 죄송합니다. 감사. – glomad

+0

예! jquery를 사용할 수 있습니다. – Caipivara

답변

0

... 모든 사람에게

감사합니다 ...

jQuery.fn.replaceEachOne = function (objective, reposition) { 
    this.contents().each(function(){ 
     if (this.nodeType == Node.ELEMENT_NODE) { 
      $(this).replaceEachOne(objective, reposition); 
     } else if (this.nodeType == Node.TEXT_NODE) { 
      var label = document.createElement("label"); 
      label.innerHTML = this.nodeValue.replace(objective, reposition); 
      this.parentNode.insertBefore(label, this); 
      this.parentNode.removeChild(this); 
     } 
    }); 
} 
1

헤더에 다른 요소가있을 것입니다. 모든 것을 새로운 텍스트로 바꾸는 것이 아닙니다.

var element = document.getElementsByTagName('html')[0]; 
element.innerHTML = element.innerHTML.replace('Hi to all', 'Bye to all'); 

당신이 원하는 경우도 정규식로 대체 할 수 따옴표로 '안녕하세요 모두에게'될 것 비효율적이기는하지만 그것을 할 것이

$(function(){ 
    var element = $('body, head').find('*:contains("Hi to all")'); 
    element.html('Bye to all'); 
}); 
+0

아니요, 작동하지 않습니다 :(. 콘텐츠에 "안녕하세요"이상을 포함 할 수 있습니다. – Caipivara

+0

콘텐츠 가져 오기 var content = element.html(); 그런 다음 문자열 바꾸기 content.replace ('Hi' , 'Bye'); –

+0

그게 내가하고있는 일이야 !!! – Caipivara

0

한 가지 방법 같은 것을 사용해보십시오.

+0

나는 이해하지 못한다. 제발 좀 더 설명해 주시겠습니까? document.getElementsByTagName ('html') [0] – Caipivara

+0

@Daniel GR document.getElementsByTagName()는 주어진 태그 이름이 'html'인 모든 요소에 대한 포인터의 배열을 반환합니다. 그 중 하나만 있기 때문에 첫 번째 요소 ([0]) 만 가져오고 element.innerHTML을 지정할 수 있습니다 두 번째 줄은 element.innerHTML에 replace 메소드를 사용하고 element.innerHTML에이를 할당합니다. – eds

1

태그를 변경하지 않으려면 왜 텍스트를 수정하지 않고 "html"을 수정해야합니까? 마지막으로

 
jQuery.fn.replaceEachOne = function (objective, rep) { 
    return this.each(function(){ 
       $(this).text($(this).text().replace(new RegExp('(\\s'+objective+'\\s(?![[\\w\\s?&.\\/;#~%"=-]*>]))', "ig"), rep)); 
      } 
     ); 
} 
+0

모든 태그를 지우고 텍스트를 다음과 같이 넣으면 작동하지 않습니다. 텍스트! p와 같은 페이지 apears 빈 텍스트! 또는 내가 뭘 잘못하고있는거야? – Caipivara

+0

.. 오케이. 그것을 테스트하지 않았다. –

관련 문제