WebBrowser 注入 Javascript 将焦点设置到第一个输入框 [英] WebBrowser Inject Javascript To Set the Focus to the First Input Box
问题描述
我想注入一个 javascript 以将焦点设置到第一个输入框.
I would like to inject a javascript in order to set the focus to the first input box.
我当前的代码是这样的:
My current code is this:
Me.WebBrowser1.Focus()
Dim i&
Dim JS(100) As String
'The following Javascript-injection ensures, that the first
'found input-element (if there is one) will be focused
' i = i + 1 : JS(i) = "<script>"
i = i + 1 : JS(i) = "var inputElements = document.getElementsByTagName('input');"
i = i + 1 : JS(i) = "for(i=0; i<inputElements.length; i++)"
i = i + 1 : JS(i) = "{"
i = i + 1 : JS(i) = " if (inputElements[i].type != 'hidden')"
i = i + 1 : JS(i) = " {"
i = i + 1 : JS(i) = " if (inputElements[i].disabled == false)"
i = i + 1 : JS(i) = " {"
' If uSetFocusToFirstBox Then
i = i + 1 : JS(i) = " inputElements[i].focus();"
' End If
'If uScrollIntoView Then
i = i + 1 : JS(i) = " inputElements[i].scrollIntoView(true);"
'End If
i = i + 1 : JS(i) = " break;"
i = i + 1 : JS(i) = " }"
i = i + 1 : JS(i) = " }"
i = i + 1 : JS(i) = "}"
Dim head As HtmlElement = Me.WebBrowser1.Document.GetElementsByTagName("head")(0)
Dim scriptEl As HtmlElement = Me.WebBrowser1.Document.CreateElement("script")
Dim element As IHTMLScriptElement = DirectCast(scriptEl.DomElement, IHTMLScriptElement)
element.text = Join(JS, "")
head.AppendChild(scriptEl)
Me.WebBrowser1.Document.InvokeScript("sayHello")
有人看到我的错误吗?
谢谢!
推荐答案
设置焦点于第一个文本输入
选项 1 - 不使用脚本
您不需要将脚本注入文档.您可以处理 DocumentCompleted
事件,然后使用 GetElementByTagName
并找到启用了 type="text"
的第一个输入,然后调用 Focus()
,它也会自动将焦点输入滚动到视图:
You don't need to inject script to the document. You can handle DocumentCompleted
event and there, find input elements using GetElementByTagName
and find the first input with type="text"
which is enabled and then call Focus()
, it also automatically scrolls the focused input to the view:
Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) _
Handles WebBrowser1.DocumentCompleted
Dim firstTextBox = Me.WebBrowser1.Document.GetElementsByTagName("input") _
.Cast(Of HtmlElement)() _
.Where(Function(element)
Return element.GetAttribute("type") = "text" And _
element.Enabled = True
End Function) _
.FirstOrDefault()
If (firstTextBox IsNot Nothing) Then
firstTextBox.Focus()
End If
End Sub
选项 2 - 注入脚本
你不需要因为这个原因使用脚本,但只是为了你需要注入脚本的情况,这里有一个例子.您应该在项目中添加对 Microsoft.mshtml
的引用并编写如下代码:
You don't need to use script for this reason, but just for case that you need to inject script, here is an example. You should add a reference to Microsoft.mshtml
to the project and write such code:
Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) _
Handles WebBrowser1.DocumentCompleted
Dim scriptText = "var inputElements = document.getElementsByTagName('input');" & _
"for(i=0; i<inputElements.length; i++){" & _
" if (inputElements[i].type == 'text' && !inputElements[i].disabled){" & _
" inputElements[i].focus();" & _
" inputElements[i].scrollIntoView(true);" & _
" break;" & _
" }" & _
"}"
Dim head As HtmlElement = Me.WebBrowser1.Document.GetElementsByTagName("head")(0)
Dim script As HtmlElement = Me.WebBrowser1.Document.CreateElement("script")
Dim scriptElement As IHTMLScriptElement = DirectCast(script.DomElement, IHTMLScriptElement)
scriptElement.text = scriptText
head.AppendChild(script)
End Sub
这篇关于WebBrowser 注入 Javascript 将焦点设置到第一个输入框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!