【问题标题】:GetElementsByTagName returns no elements in WebBrowser ControlGetElementsByTagName 在 WebBrowser 控件中不返回任何元素
【发布时间】:2011-06-22 05:09:42
【问题描述】:

这是我的代码:

    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;

谢谢

【问题讨论】:

    标签: c# javascript webbrowser-control


    【解决方案1】:

    当您将脚本放在&lt;head&gt; 中时,它会在&lt;body&gt; 的内容完全加载之前执行。有两种方法可以避免该问题:您可以将脚本放在结尾 &lt;/body&gt;-Tag 之前,或者执行您的脚本 onload

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

    【讨论】:

    • 哦,是的。你说得对。感谢您的答复。但现在所有“缩写”元素都不会出现 onclick 事件
    • 这解决了我的问题:obj.onclick = function gg() { alert(this.id) } }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-26
    • 1970-01-01
    • 2010-10-10
    • 2021-11-09
    • 2021-10-13
    相关资源
    最近更新 更多