iframe中的getElementById元素 [英] getElementById on element within an iframe
问题描述
我目前的code工作的iframe之外的元素。我应该如何获取进场通过getElementById iframe中的元素呢?我的最终目标是内部的&LT书写文字;身体ID =TinyMCE的>< P> ...< / P>< / BODY>
标签。我不使用WebBrowser控件 - 这是IEXPLORE的外部实例
HTML样品
code样品
的foreach(InternetExplorer的即新ShellWindowsClass())
{
如果(ie.LocationURL.ToString()。的IndexOf(intranet_site_url)!= - 1)
{
IWebBrowserApp WB =(IWebBrowserApp)即;
而(wb.Busy){Thread.sleep代码(100); }
HTMLDocument的文档=((HTMLDocument的)wb.Document); //取出由ID
IHTMLElement元素;
HTMLInputElementClass如果hItem; 元素=的document.getElementById(TinyMCE的);
如果hItem =(HTMLInputElementClass)元素;
hitem.value = FIRST_NAME; //取得在IFRAME ID
IHTMLFramesCollection2 hframes = document.frames;
的for(int i = 0; I< hframes.length;我++)
{
对象ref_index = I;
IHTMLWindow2设置currentFrame =(IHTMLWindow2)hframes.item(REF ref_index); 如果(设置currentFrame!= NULL)
{
MessageBox.Show(currentFrame.name);
//怎么在这里做什么?
}
其他
MessageBox.Show(空);
}
}
}
- 更新观念
下面适应我的想法的机会呢?
如果(设置currentFrame!= NULL)
{
MessageBox.Show(currentFrame.name); HTMLDocument的document_sub =((HTMLDocument的)currentFrame.document);
IHTMLElement element_sub;
HTMLInputElementClass hitem_sub; element_sub =(document_sub.getElementById(TinyMCE的));
hitem_sub =(HTMLInputElementClass)element_sub;
尝试
{
hitem_sub.value = FIRST_NAME; //上面会产生...
// InvalidCastException的:无法投类型的COM对象mshtml.HTMLBodyCLass类类型mshtml.HTMLInputElementClass
}
赶上{}
}
试试这个:
Windows.Forms.HtmlWindow帧= WebBrowser1.Document.GetElementById(decrpt_ifr)Document.Window.Frames [decrpt_ifr]。
的HtmlElement体= frame.Document.GetElementById(TinyMCE的);
body.InnerHtml =你好,世界!;
这得到框架,将其视为一个不同的文档(因为它),然后它会尝试获取其ID的元素。祝你好运。
编辑:这应该做的伎俩趁着动态
数据类型和的InternetExplorer
接口:
私人无效Form1_Load的(对象发件人,EventArgs的发送)
{
的foreach(InternetExplorer的即在新ShellWindows())
{
如果(ie.LocationURL.ToString()。的IndexOf(TinyMCE的)!= - 1)
{
IWebBrowserApp WB =(IWebBrowserApp)即;
wb.Document.Frames.Item [0] .document.body.InnerHtml =&所述p为H.;你好,世界在与下; / P>中+ DateTime.Now.ToString();
}
}
}
My current code works on elements outside of an iframe. How should I approach fetching elements within an iframe using getElementById? My end goal is to write text within the the <body id="tinymce"><p>...</p></body>
tags. I am not using a webBrowser control - this is for an external instance of iexplore
HTML Sample
Code Sample
foreach (InternetExplorer ie in new ShellWindowsClass())
{
if (ie.LocationURL.ToString().IndexOf("intranet_site_url") != -1)
{
IWebBrowserApp wb = (IWebBrowserApp)ie;
while (wb.Busy) { Thread.Sleep(100); }
HTMLDocument document = ((HTMLDocument)wb.Document);
// FETCH BY ID
IHTMLElement element;
HTMLInputElementClass hitem;
element = document.getElementById("tinymce");
hitem = (HTMLInputElementClass)element;
hitem.value = first_name;
// FETCH BY ID in IFRAME
IHTMLFramesCollection2 hframes = document.frames;
for (int i = 0; i < hframes.length; i++)
{
object ref_index = i;
IHTMLWindow2 currentFrame = (IHTMLWindow2)hframes.item(ref ref_index);
if (currentFrame != null)
{
MessageBox.Show(currentFrame.name);
// what to do from here?
}
else
MessageBox.Show("Null");
}
}
}
- update idea Chance of adapting my idea below?
if (currentFrame != null)
{
MessageBox.Show(currentFrame.name);
HTMLDocument document_sub = ((HTMLDocument)currentFrame.document);
IHTMLElement element_sub;
HTMLInputElementClass hitem_sub;
element_sub = (document_sub.getElementById("tinymce"));
hitem_sub = (HTMLInputElementClass)element_sub;
try
{
hitem_sub.value = first_name;
// the above will produce...
// InvalidCastException: Unable to cast COM object of type 'mshtml.HTMLBodyCLass' to class type 'mshtml.HTMLInputElementClass'
}
catch { }
}
Try this:
Windows.Forms.HtmlWindow frame = WebBrowser1.Document.GetElementById("decrpt_ifr").Document.Window.Frames["decrpt_ifr"];
HtmlElement body = frame.Document.GetElementById("tinymce");
body.InnerHtml = "Hello, World!";
That gets the frame and treats it as a different document (because it is) and then it tries to get the element from its id. Good luck.
Edit: This should do the trick taking advantage of the dynamic
datatype, and InternetExplorer
interface:
private void Form1_Load(object sender, EventArgs e)
{
foreach (InternetExplorer ie in new ShellWindows())
{
if (ie.LocationURL.ToString().IndexOf("tinymce") != -1)
{
IWebBrowserApp wb = (IWebBrowserApp)ie;
wb.Document.Frames.Item[0].document.body.InnerHtml = "<p>Hello, World at </p> " + DateTime.Now.ToString();
}
}
}
这篇关于iframe中的getElementById元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!