Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [inne][HTML][JavaScript]Zamiana tekstu w JS na HTML
daniel1302
post
Post #1





Grupa: Zarejestrowani
Postów: 602
Pomógł: 30
Dołączył: 1.08.2007
Skąd: Nowy Sącz

Ostrzeżenie: (0%)
-----


Witam, mam taki kod:


  1.  
  2. tmpNode.textContent = replaceText(tmpNode[0].textContent);
  3. function replaceText(text) {
  4. var stringObj = new String(text);
  5.  
  6. stringObj = stringObj.replace(/\040&\040/, "<span style=\"font-size: 80%\"> & </span>");
  7. return stringObj.replace(/\040([ąćęóńłżź\d\w]{1,2}){1}\040/gi, ' $1_');
  8. }



i zamieniam sobie ampersand na mniejszy, jednak na stronie pojawia mi sie kod HTML. Czyli jak miałem:
Cytat
Hi Ann & Monik


To po zmianie widzę:
Cytat
Hi Ann <span style="font-size: 80%"> & </span> Monik

Potrzebuję zmieniać same teksty bez kodu HTML bo w kodzie html czasem występują ampersandy

Poniżej do celów poglądowych cały kod JS
  1. var spans = document.getElementById('rules').getElementsByTagName('span');
  2.  
  3. function replaceText(text) {
  4. var stringObj = new String(text);
  5.  
  6. stringObj = stringObj.replace(/\040&\040/, "<span style=\"font-size: 80%\"> & </span>");
  7. return stringObj.replace(/\040([ąćęóńłżź\d\w]{1,2}){1}\040/gi, ' $1_');
  8. }
  9.  
  10. function replaceTextInDomElements(elements, lvl) {
  11. var elementsLength = elements.length;
  12.  
  13. var tmpNode = null;
  14. for (var i=0; i< elementsLength; i++) {
  15. tmpNode = elements[i].childNodes;
  16.  
  17.  
  18. if (typeof tmpNode[0] != 'undefined' && typeof tmpNode[0].textContent != 'undefined') {
  19. tmpNode[0].textContent = replaceText(tmpNode[0].textContent);
  20. }
  21.  
  22. if (tmpNode.length > 1) {
  23. replaceTextInDomElements(tmpNode, (lvl+1));
  24. }
  25. }
  26.  
  27. }
  28. replaceTextInDomElements(spans, 0);
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 20.08.2025 - 05:18