作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的 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 添加新输入。
感谢回复!
最佳答案
尝试像这样附加点击处理程序,而不是在 onload
和 attach
函数中使用 setAttribute
:
document.getElementById('addDestination').onclick = addDest;
关于c# - 如何让 JavaScript 在 WebBrowser 控件中运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7345217/
我是一名优秀的程序员,十分优秀!