WPF WebBrowser:如何设置元素单击事件?
我已经弄清楚如何在页面加载完成后立即将所有内容变为红色:
private void webBrowser1_LoadCompleted(object sender, NavigationEventArgs e)
{
var doc = (IHTMLDocument2)webBrowser1.Document;
foreach (IHTMLElement elem in doc.all)
{
elem.style.backgroundColor = "#ff0000";
}
}
现在,如果我想让元素仅在单击时改变颜色怎么办?我看到 elem
有一个 onclick
属性,但它的类型是 dynamic
所以我不知道如何处理它。 文档毫无用处。
I've figured out how to make everything red as soon as the page is finished loading:
private void webBrowser1_LoadCompleted(object sender, NavigationEventArgs e)
{
var doc = (IHTMLDocument2)webBrowser1.Document;
foreach (IHTMLElement elem in doc.all)
{
elem.style.backgroundColor = "#ff0000";
}
}
Now what if I want to make the element only change color when it's clicked? I see that elem
has an onclick
property, but it's type is dynamic
so I don't know what to do with it. The documentation is pretty useless.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
HTMLDocumentClass
而不是IHTMLDocument2
接口来实现这一点:上述解决方案有一个缺点:
onclick
事件不会冒泡,即使返回false
(即单击超链接不会导航到其他页面)。You could do it by using the
HTMLDocumentClass
instead of theIHTMLDocument2
interface:The above solution, has one drawback: the
onclick
event does not bubble, even thoughfalse
is returned (i.e. clicking at hyperlinks does not navigate to other pages).