Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在节点innerText中搜索匹配字符串,并将匹配项包装在innerHTML标记中_Javascript_Jquery_Html - Fatal编程技术网

Javascript 在节点innerText中搜索匹配字符串,并将匹配项包装在innerHTML标记中

Javascript 在节点innerText中搜索匹配字符串,并将匹配项包装在innerHTML标记中,javascript,jquery,html,Javascript,Jquery,Html,我正在寻找一种方法来搜索节点的innerText以查找匹配的字符串。然后将匹配的文本包装在中 要求: 1>节点可能包含html标记,我希望这些标记保留在匹配的文本选择中。 2>我还希望匹配文本区分大小写。 例: 这是我要搜索的内容。 我想查找“我要搜索的内容” 匹配并包装“我的内容”以搜索“,结果如下: This is <span>my <strong>content</strong> to search</span>. 这是我要搜索的内容。

我正在寻找一种方法来搜索节点的innerText以查找匹配的字符串。然后将匹配的文本包装在

要求:

1>节点可能包含html标记,我希望这些标记保留在匹配的文本选择中。

2>我还希望匹配文本区分大小写。

例:

这是我要搜索的内容。
我想查找“我要搜索的内容”

匹配并包装
“我的内容”以搜索“
,结果如下:

This is <span>my <strong>content</strong> to search</span>.
这是我要搜索的内容。
此代码仅搜索节点内的精确匹配,如果存在html标记(因为这些是附加的子节点),则不匹配:

/*搜索提示*/
var text=prompt(“搜索:,”);
if(text==null | | text.length==0)返回;
/*删除突出显示的结果*/
var span=document.getElementsByClassName(“labnol”);
if(跨度){
对于(变量i=0;i=0){
span节点=document.createElement(“span”);
setAttribute(“类”、“标签”);
spannode.style.backgroundColor=“黄色”;
中间位=节点.拆分文本(pos);
endbit=middlebit.splitText(len);
middleclone=middlebit.cloneNode(真);
spannode.appendChild(middleclone);
middlebit.parentNode.replaceChild(spannode,middlebit);
skip=1;
}
}如果(node.nodeType==1&&node.childNodes&&node.tagName.toUpperCase()!=“脚本”&&node.tagName.toUpperCase!=“样式”){
对于(var child=0;child
This is <span>my <strong>content</strong> to search</span>.
/* Prompt for search */
    var text = prompt("Search for:", "");
    if (text == null || text.length == 0) return;

    /* Remove highlighted results */
    var spans = document.getElementsByClassName("labnol");

    if (spans) {
        for (var i = 0; i < spans.length; i++) {
            spans[i].style.backgroundColor = "transparent";
        }
    }

    function searchWithinNode(node, te, len) {
        var pos, skip, spannode, middlebit, endbit, middleclone;

        skip = 0;
        if (node.nodeType == 3) {
            pos = node.data.indexOf(te);

            if (pos >= 0) {
                spannode = document.createElement("span");
                spannode.setAttribute("class", "labnol");
                spannode.style.backgroundColor = "yellow";
                middlebit = node.splitText(pos);
                endbit = middlebit.splitText(len);
                middleclone = middlebit.cloneNode(true);
                spannode.appendChild(middleclone);
                middlebit.parentNode.replaceChild(spannode, middlebit);
                skip = 1;
            }
        } else if (node.nodeType == 1 && node.childNodes && node.tagName.toUpperCase() != "SCRIPT" && node.tagName.toUpperCase != "STYLE") {
            for (var child = 0; child < node.childNodes.length; ++child) {
                child = child + searchWithinNode(node.childNodes[child], te, len);
            }
        }
        return skip;
    }
    searchWithinNode(document.body, text, text.length);