Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [inne][HTML][JavaScript]Zamiana tekstu w JS na HTML
daniel1302
post 10.03.2015, 11:19:51
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
com
post 10.03.2015, 12:33:05
Post #2





Grupa: Zarejestrowani
Postów: 3 034
Pomógł: 366
Dołączył: 24.05.2012

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


po pierwsze ampersand się powinno pisać nie & tylko &amp; po drugie skoro z obiektu robisz stringa to on to traktuje jak tekst a nie jak znacznik html smile.gif
Go to the top of the page
+Quote Post

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 Wersja Lo-Fi Aktualny czas: 5.05.2025 - 05:54