【问题标题】:Is it possible to pick up DOM element inside of iFrame on click?点击时是否可以在 iFrame 内拾取 DOM 元素?
【发布时间】:2011-05-23 02:52:11
【问题描述】:

我正在使用C# .NET Framework 2.0 创建应用程序,该应用程序可以在 WebBrowser 控件中获取用户单击的 DOM 元素部分。

到目前为止,我可以在 WebBrowser 控件的 HTML 中选择 html 标记及其属性。

//adding event when user mouse down or focus out
webBrowser1.Document.MouseDown += new HtmlElementEventHandler(myMouseDown);

这些事件会将 DOM 信息输出到列表框等

 private void myMouseDown(object sender, HtmlElementEventArgs e)
 {
    HtmlElement tag = webBrowser1.Document.GetElementFromPoint(e.ClientMousePosition);

    txtRecord.Items.Add("TagName=" + tag.TagName);
    txtRecord.Items.Add("id=" + tag.GetAttribute("id"));
    txtRecord.Items.Add("name=" + tag.GetAttribute("name"));
    txtRecord.Items.Add("type=" + tag.GetAttribute("type"));
    txtRecord.Items.Add("value=" + tag.GetAttribute("value"));
    txtRecord.Items.Add("class=" + tag.GetAttribute("class"));
    txtRecord.Items.Add("inner text=" + tag.InnerText);

 }

但是随后 mouseDown 事件在 iFrame 中不起作用。

当我点击 iFrame 内部时,它不会吐出 DOM 信息。

是否可以通过用户在 iFrame 中点击该点来获取 DOM 信息?

更新:

找到 HtmlWindow 对象并应用相同的事件逻辑。

HtmlWindow iFrame;
iFrame = webBrowser1.Document.Window.Frames[0];
iFrame.Document.MouseDown += new HtmlElementEventHandler(myMouseDown);

但是最后一行抛出 UnauthorizedAccessException :(

【问题讨论】:

    标签: c# .net iframe browser


    【解决方案1】:

    我找到了为什么会收到 UnauthorizedAccessException。这是因为我的主页是 http://localhost/index.html 而 iFrame src 违反了跨框架脚本安全性,根据:

    http://msdn.microsoft.com/en-us/library/ms171715.aspx
    http://msdn.microsoft.com/en-us/library/ms533028.aspx
    

    我已将 iFrame src 更改为使用相同的域,http://localhost/secondPage.html

    然后它开始工作!

    【讨论】:

      猜你喜欢
      • 2018-08-24
      • 1970-01-01
      • 1970-01-01
      • 2018-07-09
      • 1970-01-01
      • 2015-01-12
      • 2018-07-13
      • 2011-12-29
      • 1970-01-01
      相关资源
      最近更新 更多