c# IHTMLElement对象如何绑定js onclick事件

fuillusion 2011-11-14 03:29:52
如题,使用webbrowser控件时。我需要将下载下来页面进行动态绑定脚本。现在不知道如何绑定onclick事件,代码如下:

object index = 0;
IHTMLWindow2 htmlWindow = (IHTMLWindow2)(((HTMLDocumentClass)(webBrowser1.Document.DomDocument)).frames.item(ref index));
IHTMLDocument2 htmlDoc = CrossFrameIE.GetDocumentFromWindow(htmlWindow);
//处理链接
foreach (IHTMLElement htmlEl in htmlDoc.links)
{
_href = htmlEl.getAttribute("href", 1).ToString().ToLower();
if (Regex.IsMatch(_href, "downloadfile"))
{
htmlEl.setAttribute("href", "javascript:alert('本机不支持下载,谢谢合作!')", 1);
}
}
//处理文本
foreach (IHTMLElement htmlInputEl in (IHTMLElementCollection)htmlDoc.all.tags("INPUT"))
{
HtmlElement _HtmlEl = htmlInputEl as HtmlElement;
_text = _HtmlEl.GetAttribute("type");
if (_text.Equals("text"))//如果是文本的话
{
//如何进行动态绑定js脚本事件
}

}
...全文
471 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
ddddddddddddddddddddd一下 IHTMLElement::getAttribute 的第一个参数有哪些可以用的字符串啊?除了“href”
阿非 2011-11-14
  • 打赏
  • 举报
回复
htmlEl += new
HTMLElementEvents2_onclickEventHandler(HtmlElement1_Click);

private void HtmlElement1_Click(Object sender, HtmlElementEventArgs e) {}

fuillusion 2011-11-14
  • 打赏
  • 举报
回复
不好意思,上面的代码,有个地方写错了

//HtmlElement _HtmlEl = htmlInputEl as HtmlElement;
//_text = _HtmlEl.GetAttribute("type");
_text=htmlEl.getAttribute("type", 1)


htmlEl对象是IHTMLElement类型的,没有click事件,只有onclick属性
阿非 2011-11-14
  • 打赏
  • 举报
回复
private void HtmlElement1_Click(Object sender, HtmlElementEventArgs e) {}

阿非 2011-11-14
  • 打赏
  • 举报
回复
我之前没注意 htmlDoc.all.tags("INPUT"))

你是要给 input type='text' 文本框设置click 是么

定义如下签名的方法
private void HtmlElement1_Click(Object sender, HtmlElementEventArgs e) {)

fuillusion 2011-11-14
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 sandy945 的回复:]
你应该给文本加个容器 eg: span ,div 之类的

然后给它们绑定事件
[/Quote]
能给出具体代码吗?我知道当前该元素有一个onclick属性,但不知道如何去实现该属性
阿非 2011-11-14
  • 打赏
  • 举报
回复
你应该给文本加个容器 eg: span ,div 之类的

然后给它们绑定事件

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧