jQuery循环浏览文本内容,同时保持包装html标签

时间:2013-10-30 17:43:35

标签: javascript jquery

我有以下javascript函数,旨在将BIDI代码附加到与特定条件匹配的文本。当前函数的问题在于它对于仅文本内容很有效,但对于具有HTML内容的内容存在问题。总而言之,我将文本拆分为单词并检查每个单词中的任何字符是否与某个标准匹配,以便将BIDI代码附加到其中。

当前状态:

<div>This is the text that I am processing</div> //This works great

<div><span>This is the text</span><p>that I am processing</p></div> // This doesn't work well.

我希望该函数可以用于文本,但也要将包装HTML标记保留在其位置以便保留

等....

function flipper(flipselector) {
        var pagetitle = $(flipselector);
        var text = pagetitle.text().split(' '); //I know that I am using text function here but .html didn't work either
        var newtext="";
    for( var i = 1, len = text.length; i < len; i=i+1 ) {
        //text[i] = '<span>' + text[i] + '</span>';
        newstring="";

        if (matches = text[i].match(/\d/))
        {
            var currentstring=text[i];
            for (var x = 0, charlen = currentstring.length; x < charlen; x++) {

                if (matches = currentstring[x].match(/\d/)) {
                    varnewchar=currentstring[x];
                }else {
                    varnewchar= "&rlm;" + currentstring[x];    
                }

                newstring=newstring + varnewchar;
            }

        } else {
            newstring= text[i];
        }

          newtext=newtext + " " + newstring;
    }
    pagetitle.html(newtext);
    }

0 个答案:

没有答案