c# webBrowser控件与js的交互

知识:

[c-sharp] view plain copy print ?
  1. [System.Runtime.InteropServices.ComVisibleAttribute(true)] 
  2. 这是为了将该类设置为com可访问 
  3.  
  4. Url属性:WebBrowser控件显示的网页路径  
  5.  
  6. ObjectForScripting属性:该对象可由显示在WebBrowser控件中的网页所包含的脚本代码访问 
  7. JavaScript通过window.external调用C#公开的方法。即由ObjectForScripting属性设置的类的实例中所包含的公共方法。 
  8.  
  9.   
  10. // WebBrowser控件显示的网页路径 
  11. webBrowser1.Navigate(new Uri(System.Environment.CurrentDirectory + @"/XXX.html", UriKind.RelativeOrAbsolute)); 
  12. // 将当前类设置为可由脚本访问 
  13. webBrowser1.ObjectForScripting = this

.CS

[c-sharp] view plain copy print ?
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5. using System.Windows; 
  6. using System.Windows.Controls; 
  7. using System.Windows.Data; 
  8. using System.Windows.Documents; 
  9. using System.Windows.Input; 
  10. using System.Windows.Media; 
  11. using System.Windows.Media.Imaging; 
  12. using System.Windows.Navigation; 
  13. using System.Windows.Shapes; 
  14. using System.Web; 
  15. using System.Security.Permissions; 
  16. namespace WpfApplication1 
  17.     /// <summary> 
  18.     /// Interaction logic for Window1.xaml 
  19.     /// </summary> 
  20.     public partial class Window1 : Window 
  21.     { 
  22.         public Window1() 
  23.         { 
  24.             InitializeComponent(); 
  25.             Basic ds = new Basic (); 
  26.             webBrowser1.Navigate(new Uri(System.Environment.CurrentDirectory + @"/aaa.html", UriKind.RelativeOrAbsolute));//获取根目录的日历文件 
  27.             webBrowser1.ObjectForScripting = ds;//该对象可由显示在WebBrowser控件中的网页所包含的脚本代码访问 
  28.         } 
  29.         private void Button_Click(object sender, RoutedEventArgs e) 
  30.         { 
  31.             textBox1.Text = DoSomething.name; 
  32.         } 
  33.  
  34.     } 
  35.     [System.Runtime.InteropServices.ComVisibleAttribute(true)]//将该类设置为com可访问 
  36.     public class Basic 
  37.     { 
  38.         public static string name; 
  39.         public string Name 
  40.         { 
  41.             get { return name; } 
  42.             set { name = value; } 
  43.         } 
  44.         public void ClickEvent(string str) 
  45.         { 
  46.             this.Name = str; 
  47.         } 
  48.     } 

HTML

[c-sharp] view plain copy print ?
  1. <HTML> 
  2. <head> 
  3. <mce:script language="JavaScript" type="text/javascript"><!-- 
  4. function Selec() 
  5. var divV=document.getElementById('div2').innerText; 
  6. window.external.ClickEvent(divV); 
  7. // --></mce:script> 
  8. </head> 
  9. <Body> 
  10. <Form> 
  11. <div id="div1" onClick="Selec();">000000000000</div> 
  12. <div id="div2">111111</div> 
  13. </Form> 
  14. </Body> 
  15. </HTML> 

如果需要在运行时点击按钮后再将值传入页面显示,则用下列方法传值

this.webBrowser1.InvokeScript("js中的函数",“要传的值”);

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值