我正在尝试使用IE和AHK在页面上的div中插入一个按钮。我可以执行按钮插入,但如何让onclick工作
这就是我所拥有的:
SetTitleMatchMode, 2
wb := IEGet("Page1")
inp := wb.document.createElement("input")
typ := wb.document.createAttribute("type")
typ.value := "button"
val := wb.document.createAttribute("value")
val.value := "Click Me!"
btnclick := wb.document.createAttribute("onclick")
btnclick.value := "alert(123)"
inp.setAttributeNode(typ)
inp.setAttributeNode(val)
inp.setAttributeNode(btnclick)
wb.document.all.external.appendChild(inp)
IEGet(Name="") ;Retrieve pointer to existing IE window/tab
{
IfEqual, Name,, WinGetTitle, Name, ahk_class IEFrame
{
Name := ( Name="New Tab - Windows Internet Explorer" ) ? "about:Tabs" : RegExReplace( Name, " - (Windows|Microsoft) Internet Explorer" )
}
For wb in ComObjCreate( "Shell.Application" ).Windows
{
If ( wb.LocationName = Name ) && InStr( wb.FullName, "iexplore.exe" )
{
Return wb
}
}
}
div的id为“external”。我使用的是IE 11,Win 8.1和AHK 1.1.19.01。页面标题是“Page1”。
当我运行上面的脚本时,我得到了按钮。我检查它看起来像这样:
<input onclick="alert(123)" type="button" value="Click Me!">
当我点击它时,如何让按钮实际触发动态创建的onclick属性?这就好像IE不会将其识别为要处理的事件。
答案 0 :(得分:0)
您可以尝试使用.Click()
方法触发点击事件。
使用示例:
wb.document.getElementsById("ExampleButton").Click()
了解有关Click方法(MDN)的更多信息:
https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement.click