是否可以检测在 WP7 WebBrowser 控件中点击的 DOM 元素? [英] Is it possible to detect the DOM element tapped in a WP7 WebBrowser control?
问题描述
是否可以使用 WP7 WebBrowser 控件检测用户在网站上点击的位置下的 DOM 元素?我想让用户识别呈现页面的某些部分.
Is it possible to detect the DOM elements under a spot the user taps on a website using the WP7 WebBrowser control? I would like to let the user identify certain sections of the rendered page.
推荐答案
是的.这段代码对我来说效果很好:
Yes. This code works pretty well for me:
private void button1_Click(object sender, RoutedEventArgs e)
{
// first define a new function which serves as click handler
webBrowser1.InvokeScript("eval", "this.newfunc_eventHandler = function(e) { window.external.notify(e.srcElement.tagName); }");
// attach function to body
webBrowser1.InvokeScript("eval", "document.body.addEventListener('click', newfunc_eventHandler, false);");
}
private void webBrowser1_ScriptNotify(object sender, NotifyEventArgs e)
{
// this should be called every time you tap an element; the value should be its tag name
Debug.WriteLine(e.Value);
}
它遵循 Justin 的回答(他在编码时打败了我).它首先定义一个点击处理程序,然后附加到页面的正文(全部通过 InvokeScript
).每次点击元素 ScriptNotify
应该在 WebBrowser
上调用,报告被点击元素的标签名称.
It goes along Justin's answer (who beat me while coding). It first defines a click handler which then is attached to the page's body (all via InvokeScript
). Every time you tap an element ScriptNotify
should be called on the WebBrowser
reporting the tapped element's tag name.
确保您已为浏览器控件设置了 IsScriptEnabled
为 true.
Make sure you have set IsScriptEnabled
true for your browser control.
这篇关于是否可以检测在 WP7 WebBrowser 控件中点击的 DOM 元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!