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?
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.
Try this:
$("body *").each(function() {
if ( this.childElementCount > 0 ) { return; }
$(this).text(function(i, v) { return v.replace('1','۱'); });
});
Update: doesn't work...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With