在线等,webbrowser如何往js传递数值
展开全部
winform的webbrowser装载htm文件,然后和winform窗体交互。
1.page.htm文件源代码
<!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>
<title></title>
<script type="text/javascript">
<!--
function F1(obj)
{
document.all["Country"].innerText=obj;
}
function F2()
{
document.all["Country"].innerText="法国";
}
-->
</script>
</head>
<body bottommargin="0" leftmargin="0" topmargin="0" rightmargin="0">
<table cellspacing="0" cellpadding="0" width="94%" border="0">
<tr>
<td style="width: 32px" align="center" bgcolor="#f0f8ff" height="26">
<font color="#ff0000">1</font></td>
<td style="width: 225px" align="center" bgcolor="#f0f8ff" height="26">
<div id="Country">
中国</div>
</td>
</tr>
</table>
</body>
</html>
2.TestForm源代码
private void TestForm_Load(object sender, EventArgs e)
{
webBrowser1.Url = new Uri("C:/page.htm");//指定要载入的网页
}
private void ButtonF1(object sender, EventArgs e)
{
mshtml.IHTMLDocument2 currentDoc = (mshtml.IHTMLDocument2)webBrowser1.Document.DomDocument;
mshtml.IHTMLWindow2 win = (mshtml.IHTMLWindow2)currentDoc.parentWindow;
win.execScript("F1('日本')", "javascript");//调用函数F1
}
private void ButtonF2(object sender, EventArgs e)
{
mshtml.IHTMLDocument2 currentDoc = (mshtml.IHTMLDocument2)webBrowser1.Document.DomDocument;
mshtml.IHTMLWindow2 win = (mshtml.IHTMLWindow2)currentDoc.parentWindow;
win.execScript("F2()", "javascript");//调用函数F2
}
private void ButtonF3(object sender, EventArgs e)
{
mshtml.IHTMLDocument2 currentDoc = (mshtml.IHTMLDocument2)webBrowser1.Document.DomDocument;
mshtml.IHTMLElement el = (mshtml.IHTMLElement)win.document.all.item("Country", null);//取得页面上的Country Dom对象
el.innerText="荷兰";//直接修改页面上Country对象的属性
}
//调用webbrowser的DocumentCompleted事件来自动修改页面属性,修改属性的方法可以用上面的三种方法。我用的是第一种方法
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
mshtml.IHTMLDocument2 currentDoc = (mshtml.IHTMLDocument2)webBrowser1.Document.DomDocument;
mshtml.IHTMLWindow2 win = (mshtml.IHTMLWindow2)currentDoc.parentWindow;
win.execScript("F1('日本')", "javascript");//调用函数F1
}
1.page.htm文件源代码
<!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>
<title></title>
<script type="text/javascript">
<!--
function F1(obj)
{
document.all["Country"].innerText=obj;
}
function F2()
{
document.all["Country"].innerText="法国";
}
-->
</script>
</head>
<body bottommargin="0" leftmargin="0" topmargin="0" rightmargin="0">
<table cellspacing="0" cellpadding="0" width="94%" border="0">
<tr>
<td style="width: 32px" align="center" bgcolor="#f0f8ff" height="26">
<font color="#ff0000">1</font></td>
<td style="width: 225px" align="center" bgcolor="#f0f8ff" height="26">
<div id="Country">
中国</div>
</td>
</tr>
</table>
</body>
</html>
2.TestForm源代码
private void TestForm_Load(object sender, EventArgs e)
{
webBrowser1.Url = new Uri("C:/page.htm");//指定要载入的网页
}
private void ButtonF1(object sender, EventArgs e)
{
mshtml.IHTMLDocument2 currentDoc = (mshtml.IHTMLDocument2)webBrowser1.Document.DomDocument;
mshtml.IHTMLWindow2 win = (mshtml.IHTMLWindow2)currentDoc.parentWindow;
win.execScript("F1('日本')", "javascript");//调用函数F1
}
private void ButtonF2(object sender, EventArgs e)
{
mshtml.IHTMLDocument2 currentDoc = (mshtml.IHTMLDocument2)webBrowser1.Document.DomDocument;
mshtml.IHTMLWindow2 win = (mshtml.IHTMLWindow2)currentDoc.parentWindow;
win.execScript("F2()", "javascript");//调用函数F2
}
private void ButtonF3(object sender, EventArgs e)
{
mshtml.IHTMLDocument2 currentDoc = (mshtml.IHTMLDocument2)webBrowser1.Document.DomDocument;
mshtml.IHTMLElement el = (mshtml.IHTMLElement)win.document.all.item("Country", null);//取得页面上的Country Dom对象
el.innerText="荷兰";//直接修改页面上Country对象的属性
}
//调用webbrowser的DocumentCompleted事件来自动修改页面属性,修改属性的方法可以用上面的三种方法。我用的是第一种方法
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
mshtml.IHTMLDocument2 currentDoc = (mshtml.IHTMLDocument2)webBrowser1.Document.DomDocument;
mshtml.IHTMLWindow2 win = (mshtml.IHTMLWindow2)currentDoc.parentWindow;
win.execScript("F1('日本')", "javascript");//调用函数F1
}
参考资料: http://blog.csdn.net/cnrefresh/article/details/2328455
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询