2011-05-23 37 views
0

我正在使用C# .NET Framework 2.0來創建應用程序,可以用戶在WebBrowser控件中選取DOM元素的點擊部分。單擊時可以在iFrame內部拾取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); 

} 

但隨後發生的事件鼠標按下不工作的iFrame。

當我點擊iFrame內部時,它不會吐出DOM信息。

是否有可能通過用戶點擊iFrame中的該點來拾取DOM信息?

更新:

找到HtmlWindow對象並應用相同的事件邏輯。

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

但隨後的最後一行拋出UnauthorizedAccessException :(

回答

0

我發現了,爲什麼我得到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

然後開始工作!

相關問題