gpt4 book ai didi

c# - 如何让 JavaScript 在 WebBrowser 控件中运行?

转载 作者:行者123 更新时间:2023-11-29 15:03:08 25 4
gpt4 key购买 nike

在我的 winforms 应用程序中,我有一个名为 webBrowser1 的 WebBrowser 控件。

在代码中,我添加的只是导航到一个页面:

private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("http://localhost:6489/Default.aspx");
}

我导航到的页面的代码是:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TableRowShow.Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
window.onload = function()
{
document.getElementById('addDestination').setAttribute('onclick', 'addDest();');
}

function attach()
{
document.getElementById('addDestination').setAttribute('onclick', 'addDest();');
}

var i = 1; // position of next tr to be shown

function addDest()
{
var trs = document.getElementById('travelTable').getElementsByTagName('tr');

if (trs[i] != null)
trs[i++].style.display = "";
}
</script>
</head>
<body>
<form id="form1" runat="server">
<table id="travelTable">
<tr>
<td>
<asp:TextBox runat="server" />
</td>
</tr>
<tr style="display: none">
<td>
<asp:TextBox runat="server" />
</td>
</tr>
<tr style="display: none">
<td>
<asp:TextBox runat="server" />
</td>
</tr>
</table>
<asp:HyperLink runat="server" ID="addDestination"
ClientIDMode="Static" NavigateUrl="javascript:;" >
Add Destination
</asp:HyperLink>
</form>
</body>
</html>

在 IE 或 Chrome 等浏览器中看到的页面如下所示:

点击添加目标 anchor 创建一个新的输入:

我在使用 WebBrowser 控件时遇到的问题是它加载了页面但 JavaScript 不起作用。

如果我点击“添加目标”,即使同一页面在 Chrome 或 IE 中运行良好,也没有任何反应。

放置断点并使用:

webBrowser1.Document.InvokeScript("addDestination");

在立即窗口内,然后继续运行程序会激活该函数中的 JavaScript 添加新输入。

感谢回复!

最佳答案

尝试像这样附加点击处理程序,而不是在 onloadattach 函数中使用 setAttribute:

document.getElementById('addDestination').onclick = addDest;

关于c# - 如何让 JavaScript 在 WebBrowser 控件中运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7345217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com