父页面的常用HTML结构(index.html
):
<html>
<head>
<title>test title</title>
</head>
<frameset cols="150,*">
<frame name="left" src="testleft.html">
</frame>
<frame name="right" src="testright.html">
</frame>
</frameset>
</html>
子页面的常用HTML结构(teSTLeft.html
):
<html>
<head>
<script>
function proc1(frm) {
alert("called proc1");
frm.setAttribute("id", "change!!");
}
</script>
</head>
<body>
<p>This is testpage.</p>
<form name="TestFormName" action="get" id="TestFormId">
<input type="text" value="TextValueEmpty" />
</form>
</body>
</html>
我想使用 C# 在 teSTLeft.html 上调用 proc1()
。我尝试使用 WebBrowser.Document.InvokeScript()
,但它无法调用 proc1()
。我该怎么做?
最佳答案
WebBrowser.Document.InvokeScript()
只能用于调用 JScript 函数,但由于您的函数更深,我们必须创建一个小的“函数”来访问您的函数。 WebBrowser 控件没有任何方法可以做到这一点,因此我们必须使用“hack”:
我们将导航到 javascript:code
以执行我们的代码。为了到达我们的函数 proc1,我们必须使用这个小片段:top.frames[0].proc1()
。
因此,为了将它们放在一起,我们必须编写一行简单的 C# 代码:
WebBrowser.Navigate("Javascript:top.frames[0].proc1()");
关于c# - 调用子 <frame> 中指定的 JavaScript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17631533/