2011-02-06 47 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