简体   繁体   English

将html元素转换为字符串Javascript(使用搜索而非内部/外部HTML获得)

[英]Convert html element to string Javascript (obtained using search, not inner/outerHTML)

I have a JS function which searches for a string in the HTML source, and outputs the parent node: 我有一个JS函数,它在HTML源代码中搜索一个字符串,并输出父节点:

function searchHTML(searchTerm) {
    queue = [document.body],
    curr
    ;
    while (curr = queue.pop()) {
        if (!curr.textContent.match(searchTerm)) continue;
        for (var i = 0; i < curr.childNodes.length; ++i) {
            switch (curr.childNodes[i].nodeType) {
                case Node.TEXT_NODE : // 3
                    if (curr.childNodes[i].textContent.match(searchTerm)) {
                        console.log(curr);
                        // End of search
                    }
                    break;
                case Node.ELEMENT_NODE : // 1
                    queue.push(curr.childNodes[i]);
                    break;
            }
        }
    }
}

Currently, its output (in Javascript console) is not a string. 当前,其输出(在Javascript控制台中)不是字符串。

I need to perform regex on the output ( curr ), so I need it to be a string. 我需要在输出( curr )上执行正则表达式,因此我需要将其作为字符串。

What I have tried: 我尝试过的

curr = curr.toString()

curr = curr.replace(/[0-9]/g, "")

You can use .text() jQuery function to get the string from an HTML . 您可以使用.text() jQuery函数从HTML获取字符串。

Here is an example of how you get string : 这是如何获取字符串的示例:

text= curr.text();

curr = text.replace(/[0-9]/g, "");

It seems to me you need to find the commonAncestorContainer for the term searched. 在我看来,您需要找到所搜索术语的commonAncestorContainer。 That means if the term starts in a node and ends in another, you don't really have a clear definition of the common parent, until you get the a range. 这意味着,如果该术语在一个节点中开始并在另一个节点中结束,则在获得范围之前,您实际上并没有明确的公共母体定义。

I put together the function below where you can call search('My Term') and it should get a commonAncestorContainer. 我将下面的函数放在一起,您可以在其中调用search('My Term'),它应该得到一个commonAncestorContainer。 Some tweek should still be needed to search the same term more than once and to make sure that words ending inside an element still consider that element as the parent instead of the next one after the parent. 应该仍然需要大约tweek来多次搜索相同的术语,并确保以某个元素结尾的单词仍然将该元素视为父元素,而不是父元素之后的下一个。

  var search = function (searchTerm) {

        // Stop if there is nothing to look for
        if (!searchTerm || typeof searchTerm !== 'string')
            return null;

        searchTerm = searchTerm.toLowerCase();

        var bodyText = document.body.textContent.toLowerCase(),
            range = document.createRange(),
            startOffset = bodyText.indexOf(searchTerm),
            endOffset = startOffset + searchTerm.length,
            iterationObject = {
                index: 0,
                length: bodyText.length,
                startOffset: startOffset,
                endOffset: endOffset,
                startInNodeOffset: -1,
                endInNodeOffset: -1,
                startNode: null,
                endNode: null
            };


        var textContent = function (textNode) {
            return textNode.nodeValue || textNode.textContent || textNode.wholeText;
        };

        (function iterate (node, iterationObject) {

            if (node.nodeType === 1) {

                var childNodes = node.childNodes;

                // Keep iterating but we should try to stop it when nodes are found
                for (var i = 0, iLen = childNodes.length; i < iLen; i++)
                    iterate(childNodes[i], iterationObject);

            } else if (node.nodeType === 3) {

                var text = textContent(node),
                    startInNodeOffset,
                    endInNodeOffset;

                // Change index and move on
                if (iterationObject.index + text.length < iterationObject.startOffset)
                    iterationObject.index += text.length;

                else if (iterationObject.startNode === null) {

                    startInNodeOffset = iterationObject.startOffset - iterationObject.index;

                    // Start range in the current node
                    // This condition should really only be needed to decide if the selection should start
                    // before or after this node. But that is another story.
                    if (startInNodeOffset <= text.length) {

                        iterationObject.startNode = node;
                        iterationObject.startInNodeOffset = startInNodeOffset;
                    }

                    iterationObject.index += text.length;
                } else {

                    // Now try to find the endNode
                    if (iterationObject.index + text.length < iterationObject.endOffset)
                        iterationObject.index += text.length;

                    else if (iterationObject.endNode === null) {

                        endInNodeOffset = iterationObject.endOffset - iterationObject.index;

                        if (endInNodeOffset <= text.length) {

                            iterationObject.endNode = node;
                            iterationObject.endInNodeOffset = endInNodeOffset;
                        }
                    }  
                }
            }

            if (iterationObject.startNode !== null && iterationObject.endNode !== null)
                return;

        })(document.body, iterationObject);

        if (iterationObject.startInNodeOffset > -1 && iterationObject.endInNodeOffset > -1) {

            range.setStart(iterationObject.startNode, iterationObject.startInNodeOffset);
            range.setEnd(iterationObject.endNode, iterationObject.endInNodeOffset);

            return range.commonAncestorContainer;
        }

        return null;
    };

If you are using jQuery, you can try outerHTML to get the string from the commonAncestorContainer. 如果您在使用jQuery,你可以尝试outerHTML来从commonAncestorContainer的字符串。

var parentElement = search('Whatever'),
    result = '';

if (parentElement !== null)
    result = $(parentElement).outerHTML();

You can create a temporary DOM node, and then append curr to it. 您可以创建一个临时DOM节点,然后将curr附加到该节点。 Then get the innerHTML and the result will be a string: 然后获取innerHTML ,结果将是一个字符串:

var tempNode = document.createElement("div");
tempNode.appendChild(curr);
console.log(temp.innerHTML);

Working example here: 这里的工作示例:

http://codepen.io/anon/pen/QypxwM http://codepen.io/anon/pen/QypxwM

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM