1

私はこのHTMLコードを持っています

<div class="anc-style" onclick="window.open('./view.php?a=foo')"></div>

「onclick」属性の内容を抽出したいと思います。私は次のようなことを試みました:

div.GetAttribute("onclick").ToString();

理想的には文字列を生成します

"window.open('./view.php?a=foo')"

System.__ComObject を返します。

("onclick") を ("class") に変更することでクラスを取得できますが、onclick で何が起こっているのでしょうか?

HtmlElementCollection div = webBrowser1.Document.GetElementsByTagName("div");
        for (int j = 0; j < div.Count; j++) {
            if (div[j].GetAttribute("class") == "anc-style") {
             richTextBox1.AppendText(div[j].GetAttribute("onclick").ToString());   
            }
        }
4

2 に答える 2

4

htmlDocument クラスを使用して、ドキュメント タグをプルし、以下のようなデータを抽出できます。これはほんの一例です

string htmlText = "<html><head></head><body><div class=\"anc-style\" onclick=\"window.open('./view.php?a=foo')\"></div></body></html>";

WebBrowser wb = new WebBrowser();
wb.DocumentText = "";
wb.Document.Write(htmlText);
foreach (HtmlElement hElement in  wb.Document.GetElementsByTagName("DIV"))
{
    //get start and end positions
    int iStartPos = hElement.OuterHtml.IndexOf("onclick=\"") + ("onclick=\"").Length;
    int iEndPos = hElement.OuterHtml.IndexOf("\">",iStartPos);
    //get our substring
    String s = hElement.OuterHtml.Substring(iStartPos, iEndPos - iStartPos);

    MessageBox.Show(s);
}
于 2013-01-30T23:41:56.637 に答える
1

また、使用してdiv[j]["onclick"]いるブラウザを使用してみてください。

動作するjsfiddleを作成しました。試してみて、それが機能するかどうかを確認してください

http://jsfiddle.net/4ZwNs/102/

于 2013-01-30T23:02:49.903 に答える