c# 中form调用并操作web脚本

来源:互联网 发布:硬盘检测软件 编辑:程序博客网 时间:2024/04/30 11:56

web   HTML页面图:

c 中form调用并操作web脚本 - 老黄牛 - 老黄牛 。net

 HTML(js.html) 代码:

<script type="text/javascript">
function F1(obj)
{
        document.all["Country"].innerText=obj;
}
function F2()
{
        document.all["Country"].innerText="法国";
}
</script>
<body>  
   <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>

 

form 代码:

private void button1_Click(object sender, EventArgs e)
        {
            mshtml.IHTMLDocument2 currentDoc = (mshtml.IHTMLDocument2)webBrowser1.Document.DomDocument;
            mshtml.IHTMLWindow2 win = (mshtml.IHTMLWindow2)currentDoc.parentWindow;
            win.execScript("F1('日本')", "javascript");//调用函数F1

        }

webBrowser1效果截图:

c 中form调用并操作web脚本 - 老黄牛 - 老黄牛 。net

        private void button2_Click(object sender, EventArgs e)
        {
            mshtml.IHTMLDocument2 currentDoc = (mshtml.IHTMLDocument2)webBrowser1.Document.DomDocument;
            mshtml.IHTMLWindow2 win = (mshtml.IHTMLWindow2)currentDoc.parentWindow;
            win.execScript("F2()", "javascript");//调用函数F2

        }

webBrowser1效果截图:

c 中form调用并操作web脚本 - 老黄牛 - 老黄牛 。net

          private void button3_Click(object sender, EventArgs e)
        {
            mshtml.IHTMLDocument2 currentDoc = (mshtml.IHTMLDocument2)webBrowser1.Document.DomDocument;
            mshtml.IHTMLElement el = (mshtml.IHTMLElement)currentDoc.all.item("Country", null);//取得页面上的Country Dom对象
            el.innerText = "荷兰";//直接修改页面上Country对象的属性
        }

webBrowser1效果截图:

c 中form调用并操作web脚本 - 老黄牛 - 老黄牛 。net

//用webBrowser来显示效果

private void Form6_Load(object sender, EventArgs e)
        {
            webBrowser1.Url =new Uri("js.html");

        }

 

原创粉丝点击