private void HighLightingText(HTMLDocument document, IHTMLDOMNode node, string keyword, int cnt) { // nodeType = 3:text节点 if (node.nodeType == 3) { string nodeText = node.nodeValue.ToString(); // 如果找到了关键字 if (nodeText.Contains(keyword)) { IHTMLDOMNode parentNode = node.parentNode; // 将关键字作为分隔符,将文本分离,并逐个添加到原text节点的父节点 string[] result = nodeText.Split(new string[] { keyword }, StringSplitOptions.None); for (int i = 0; i < result.Length - 1; i++) { if (result[i] != "") { IHTMLDOMNode txtNode = document.createTextNode(option[cnt] + result[i] + option[cnt]); parentNode.insertBefore(txtNode, node); } IHTMLDOMNode orgNode = document.createTextNode(option[cnt] + keyword + option[cnt]); IHTMLDOMNode hilightedNode = (IHTMLDOMNode)document.createElement("SPAN"); IHTMLStyle style = ((IHTMLElement)hilightedNode).style; style.color = "black"; style.backgroundColor = color[cnt]; hilightedNode.appendChild(orgNode); parentNode.insertBefore(hilightedNode, node); } if (result[result.Length - 1] != "") { IHTMLDOMNode postNode = document.createTextNode(option[cnt] + result[result.Length - 1] + option[cnt]); parentNode.insertBefore(postNode, node); } parentNode.removeChild(node); } // End of nodeText.Contains(keyword) } else { // 如果不是text节点,则递归搜索其子节点 IHTMLDOMChildrenCollection childNodes = node.childNodes as IHTMLDOMChildrenCollection; foreach (IHTMLDOMNode n in childNodes) { HighLightingText(document, n, keyword, cnt); } } }