<div><div><p></p></div>
<div><p></p></div></div>如何获取HMTL对应的标签

解决方案 »

  1.   

    var oDiv=document.getElementsByTagName('div');
    alert(oDiv.length)var oDiv1=document.getElementsByTagName('p');
    alert(oDiv1.length)
      

  2.   

    <div> <div> <p> </p> </div> 
    <div> <p> </p> </div> </div> 这里有多个DIV啊,好像这样不可以吧也不想用JS实现,做winform程序
      

  3.   


    <div class=class> 
    方法
    <div> <p> abc</p> </div> 
    <div> <p> def</p> </div> 
    呃呃呃
    </div> 
    比如想要上面中的
    方法
    <div> <p> abc</p> </div> 
    <div> <p> def</p> </div> 
    呃呃呃
      

  4.   

    Match m = Regex.Match(content, "<(/?)(\w+).*?>", RegexOptions.IgnoreCase | RegexOptions.Multiline);
    string aa = m.Groups[1].Value;
                这么写么,不太明白
      

  5.   


    <div class=class> 
    方法 
    <div> <p> abc </p> </div> 
    <div> <p> def </p> </div> 
    呃呃呃 
    </div> 
    。。上下都有内容,想获取class=class 的DIV标签中的内容
      

  6.   

    C# code:  Page.ClientScript.ReGisterClientScript(typeof(string),"js1","<script>var oDiv=document.getElementsByTagName('div');
    var oDiv1=document.getElementsByTagName('p');
    </script>");
     LZ 再自己扩充下
      

  7.   

    给你2个实力:方法一:使用TextRange(IHTMLTxtRange) 
    有了上一个Case的基础,相信大家立刻会想到使用TextRange。没错,TextRange除了提供查找方法之外,还提供了一个pasteHTML方法,以指定的HTML文本替换当前TextRange中的内容。代码片断如下:
        public partial class HilightDemo : Form 
         { 
            // 定义高亮显示效果的标签。 
            string tagBefore = "<span style='background-color:yellow;color:black'>"; 
            string tagAfter = "</span>";         // ……         private void btnHilight_Click(object sender, EventArgs e) 
             { 
                 HtmlDocument htmlDoc = webBrowser.Document; 
                string keyword = txtKeyword.Text.Trim(); 
                if (keyword == "") 
                    return;             object oTextRange = htmlDoc.Body.InvokeMember("createTextRange");              mshtml.IHTMLTxtRange txtrange = oTextRange as mshtml.IHTMLTxtRange;             while (txtrange.findText(keyword, 1, 4)) 
                 { 
                    try 
                     { 
                         txtrange.pasteHTML(tagBefore + keyword + tagAfter); 
                     } 
                    catch { } 
                     txtrange.collapse(false); 
                 } 
             } 
         }※这段代码里获取IHTMLTxtRange的方式和上面的例子稍稍不同,其实所谓条条大路通罗马,本质是一样的。 方法二:使用DOM(文档对象模型) 
    将HTML文档解析为DOM,然后遍历每个节点,在其中搜索关键字并进行相应替换处理即可。
        public partial class HilightDemo : Form 
         { 
            //……         private void btnHilight_Click(object sender, EventArgs e) 
             { 
                 HTMLDocument document = (HTMLDocument)webBrowser.Document.DomDocument; 
                 IHTMLDOMNode bodyNode = (IHTMLDOMNode)webBrowser.Document.Body.DomElement; 
                string keyword = txtKeyword.Text.Trim(); 
                if (keyword == "") 
                    return;              HilightText(document, bodyNode, keyword); 
             }         private void HilightText(HTMLDocument document, IHTMLDOMNode node, string keyword) 
             { 
                // 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(result[i]); 
                                 parentNode.insertBefore(txtNode, node); 
                             } 
                             IHTMLDOMNode orgNode = document.createTextNode(keyword); 
                             IHTMLDOMNode hilightedNode = (IHTMLDOMNode)document.createElement("SPAN"); 
                             IHTMLStyle style = ((IHTMLElement)hilightedNode).style; 
                             style.color = "black"; 
                             style.backgroundColor = "yellow"; 
                             hilightedNode.appendChild(orgNode);                          parentNode.insertBefore(hilightedNode, node); 
                         } 
                        if (result[result.Length - 1] != "") 
                         { 
                                 IHTMLDOMNode postNode = document.createTextNode(result[result.Length - 1]); 
                                 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) 
                     { 
                         HilightText(document, n, keyword); 
                     } 
                 } 
             } 
         } 
      

  8.   

    另外, 你可以用WebClient类的DownLoad方法. 获取到请求的字符串..
     然后用string.subString 慢慢找...
        不知道上面这几个是否跑题拉?