C# 如何使JavaScript在WebBrowser控件中运行?
在我的winforms应用程序中,我有一个名为webBrowser1的WebBrowser控件 在代码中,我添加的所有内容都是导航到一个页面:C# 如何使JavaScript在WebBrowser控件中运行?,c#,javascript,asp.net,winforms,webbrowser-control,C#,Javascript,Asp.net,Winforms,Webbrowser Control,在我的winforms应用程序中,我有一个名为webBrowser1的WebBrowser控件 在代码中,我添加的所有内容都是导航到一个页面: private void Form1_Load(object sender, EventArgs e) { webBrowser1.Navigate("http://localhost:6489/Default.aspx"); } 我导航到的页面的代码是: <%@ Page Language="C#" AutoEventWireup="t
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>
在即时窗口内,然后继续运行程序,激活该函数中的JavaScript,添加新的输入
谢谢回复 您可以尝试将属性设置为
false
以查看是否出现任何JavaScript错误。尝试像这样附加单击处理程序,而不是在onload
和attach
函数中使用setAttribute
:
document.getElementById('addDestination').onclick = addDest;
我尝试将其设置为false(在导航行之前添加),但没有出现错误。应该在哪里看到错误?据我所知,应该会出现一个消息框。这解决了问题。你知道为什么它不能在其他浏览器中使用setAttribute吗?我在windows中使用IE8,它可以正常工作。WebBrowser是否也使用IE8(安装了最新的IE)?
document.getElementById('addDestination').onclick = addDest;