JQuery change text of all elements

I need jquery to change numbers on all pages. for example, I want to change 1 to 1, so I tried this way:

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

but it will also change CSS rules and attributes. is there any trick to avoid css and attributes?

+4
source share
3 answers

This is not a job that jQuery naturally fits into. Instead of jQuery retrieving a flat list of all elements, recursively traverse the DOM tree yourself, look for text nodes to perform the replacement.

 function recursiveReplace(node) { if (node.nodeType == 3) { // text node node.nodeValue = node.nodeValue.replace("1", "Ϋ±"); } else if (node.nodeType == 1) { // element $(node).contents().each(function () { recursiveReplace(this); }); } } recursiveReplace(document.body); 

See in action here .

+9
source

Try the following:

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

Update: not working ...

+2
source

Without seeing a real example of your code, it's hard to give a good answer, but I will do my best.

Here is what I would do. I would wrap all your page numbers in a tag with a specific id like this ...

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

Then here is jQuery ...

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

You can increment values ​​in a loop to count pages or anything else!

Hope this helps,
spryno724

+1
source

All Articles