4

I need jquery to change numbers in all of pages. for example i want change 1 to ۱ so i tried this way:

$("*").each(function(){
    $(this).html(  $(this).html().replace(1,"۱")  );
})

but this will change css rules and attributes also. is there any trick to escape css and attributes?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
ShirazITCo
  • 1,041
  • 6
  • 23
  • 38

3 Answers3

10

This isn't a job that jQuery naturally fits into. Instead of getting jQuery to fetch a flat list of all elements, recursively traverse through the DOM tree yourself, searching for text nodes to perform the replace on.

function recursiveReplace(node) {
    if (node.nodeType === Node.TEXT_NODE) {
        node.nodeValue = node.nodeValue.replace("1", "۱");
    } else if (node.nodeType == Node.ELEMENT_NODE) {
        $(node).contents().each(function () {
            recursiveReplace(this);
        });
    }
}

recursiveReplace(document.body);

See it in action here.

Jeremy Moritz
  • 13,864
  • 7
  • 39
  • 43
David Tang
  • 92,262
  • 30
  • 167
  • 149
  • @Šime thanks. Just habit to include an `if-return` block in recursive functions. – David Tang Mar 31 '11 at 01:45
  • To bad that ES5's `forEach` doesn't work on NodeLists. Otherwise, we could do `node.childNodes.forEach(...)` and remove jQuery from the equation completely. – Šime Vidas Mar 31 '11 at 01:55
2

Try this:

$("body *").each(function() { 
    if ( this.childElementCount > 0 ) { return; }
    $(this).text(function(i, v) { return v.replace('1','۱'); }); 
});

Update: doesn't work...

Šime Vidas
  • 182,163
  • 62
  • 281
  • 385
1

Without seeing an actual example of your code it is hard to provide a good answer, but I will do my best.

Here is what I would do. I would wrap all of your page numbers in a tag with a particular ID, like this...

<span class="pageNumber">1</span>
...
<span class="pageNumber">n</span>

Then here is the jQuery...

$.each($("span.pageNumber"), function()) {
    $(this).text('value');
});

You could increment the values in the loop to count for pages, or anything else you want!

Hope that helps,
spryno724

James Monger
  • 10,181
  • 7
  • 62
  • 98
Oliver Spryn
  • 16,871
  • 33
  • 101
  • 195