0

当我尝试在网络浏览器中运行 javascript 时,代码会运行,但在执行后会将我带到一个右上角有一个数字值(在本例中为“1000”)的白页,让我远离所在的站点我以前是

HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
element.text = "function ScrollDown() { document.getElementsByClassName('scrollableitemclass').scrollTop = 1000 }";
head.AppendChild(scriptEl);
webBrowser1.Document.InvokeScript("ScrollDown");

感谢您的帮助

4

1 回答 1

1

您可以使用HtmlElement.ScrollIntoView滚动到您的 Html Elemet 。

看这个例子:

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            webBrowser1.DocumentText = "<html><body><span class=\"cls\" id=\"el\"> </body></html>";
        }

        void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            //for element with id
            webBrowser1.Document.GetElementById("el").ScrollIntoView(true);
            //for element with spesific
            foreach (HtmlElement el in webBrowser1.Document.All)
            {
                if (el.GetAttribute("ClassName") == "cls")
                {
                    el.ScrollIntoView(true);
                }

            }

        }


    }
于 2013-05-09T14:09:18.887 回答