Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
461 views
in Technique[技术] by (71.8m points)

jquery - Iterating through each text element in a page?

I'm trying to write a script in jQuery that would iterate through each text element inside a page. Then I need to change the color of each letter one by one. For example, for a page such as this one:

<p>Some text and <a href="http://example.com">some link</a> and <span>something else</span></p>

I would like to get:

"Some text and "
"some link"
" and "
"something else"

and be able to style each individual letter (i.e. put back into the DOM whatever I styled).

I know about the text() method but that won't do the job since it combines the text contents, while I need to access each individual text part.

Any suggestion on how to do that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. Loop through all child elements, recursively for elements.
    Store all text nodes in a list.
  2. Loop through all text nodes:
    1. Loop through the textual contents of each element.
      1. Wrap each letter in a <span> element
      2. Insert this element in a DocumentFragment
    2. Replace the text node with this fragment.

Demo: http://jsfiddle.net/B2uRn/

// jQuery plugin, example:
(function($) {
    $.fn.styleTextNodes = function() {
        return this.each(function() {
            styleTextNodes(this);
        });
    };
})(jQuery)

function styleTextNodes(element) {
    var span = document.createElement('span');
    span.className = 'shiny-letter';

    // Recursively walk through the childs, and push text nodes in the list
    var text_nodes = [];
    (function recursiveWalk(node) {
        if (node) {
            node = node.firstChild;
            while (node != null) {
                if (node.nodeType == 3) {
                    // Text node, do something, eg:
                    text_nodes.push(node);
                } else if (node.nodeType == 1) {
                    recursiveWalk(node);
                }
                node = node.nextSibling;
            }
        }
    })(element);

    // innerText for old IE versions.
    var textContent = 'textContent' in element ? 'textContent' : 'innerText';
    for (var i=text_nodes.length-1; i>=0; i--) {
        var dummy = document.createDocumentFragment()
          , node = text_nodes[i]
          , text = node[textContent], tmp;
        for (var j=0; j<text.length; j++) {
            tmp = span.cloneNode(true); // Create clone from base
            tmp[textContent] = text[j]; // Set character
            dummy.appendChild(tmp);     // append span.
        }
        node.parentNode.replaceChild(dummy, node); // Replace text node
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...