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

    技术2022-05-12  7

    web   HTML页面图:

     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效果截图:

            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效果截图:

              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效果截图:

    //用webBrowser来显示效果

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

            }

     


    最新回复(0)