开发者

JQuery change all elements text

开发者 https://www.devze.com 2023-02-20 17:36 出处:网络
I ne开发者_如何学编程ed jquery to change numbers in all of pages. for example i want change 1 to ۱ so i tried this way:

I ne开发者_如何学编程ed 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...


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号