2011-02-06 46 views
0

任何元素,這是我的代碼:的getElementsByTagName返回WebBrowser控件中

webBrowser1.ObjectForScripting = this; 

    string str = 
     "<html><head><script type=\"text/javascript\">" + 
     "var list = document.getElementsByTagName('abbr');" + 
     "len = list.length;" + 
     "for(i = 0;i < len;i++)" + 
     "{obj=list[i];obj.onclick=window.external.Test(this.id);}" + 
     "</script></head>" + 
     "<body>"; 

    for (int i = 1000; i < 1100; i++) 
    { 
     str += "<abbr id=\'" + i.ToString() + "\'" + 
     ">" + i.ToString() + " </abbr>"; 
    } 
    str += "</body></html>"; 

    webBrowser1.DocumentText = str; 

感謝

回答

2

當你放置你的腳本在<head>,前<body>的內容是完全加載它得到執行。有兩種可能性來避免這個問題:您可以在結尾</body> -Tag之前放置腳本,或者執行腳本onload

window.onload = function() { 
    // Insert code that depends on a loaded body here. 
} 
+0

哦,是的。你是對的。感謝您的答覆。但現在onclick事件並不會出現所有「簡要」元素 – ARZ 2011-02-06 11:16:08