问题是这样的:例如 <body>
<iframe width="762" scrolling="no" height="60" frameborder="no" allowtransparency="true" marginheight="0" marginwidth="0" border="0" src="ifream.html" id="lsiframe2"></iframe>
<button value="取得" onclick="getIf();">取得</button>
</body>在body中插入iframe
然后再iframe中有一个元素的id是 good 当想获取这个元素的时候 js会这样处理: ifr= document.getElementById('lsiframe2');
var gDiv= ifr.contentWindow.document.getElementById('good');但是在VC中 我用getElementById 拿到那个iframe 后 怎么再拿'good' 呢? 请给出代码! 谢谢!
<iframe width="762" scrolling="no" height="60" frameborder="no" allowtransparency="true" marginheight="0" marginwidth="0" border="0" src="ifream.html" id="lsiframe2"></iframe>
<button value="取得" onclick="getIf();">取得</button>
</body>在body中插入iframe
然后再iframe中有一个元素的id是 good 当想获取这个元素的时候 js会这样处理: ifr= document.getElementById('lsiframe2');
var gDiv= ifr.contentWindow.document.getElementById('good');但是在VC中 我用getElementById 拿到那个iframe 后 怎么再拿'good' 呢? 请给出代码! 谢谢!
参看:http://www.codeguru.com/cpp/i-n/internet/browsercontrol/article.php/c130652) iframe对应的IHTMLDocument2, QueryInterface得到 IHTMLDocument3
然后调用 IHTMLDocument3 getElementById() 得到 good元素对应的 IHTMLElement
CComPtr<IHTMLDocument2> pTop;
pDocument->QueryInterface(IID_IHTMLDocument2, (void**)&pTop);
IHTMLDocument2* pframeDocument;
if (pTop)
{
getFrameDocumentById(pTop, DICT_UPDATE_ID, &pframeDocument);
}
CComPtr<IHTMLDocument3> pFrame;
pframeDocument->QueryInterface(IID_IHTMLDocument3, (void**)&pFrame);
pframeDocument->Release();
CComPtr<IHTMLElement> elem;
if (pFrame)
{
pFrame->getElementById(CComBSTR(L"xixiking"), &elem);
}
if (elem)
{
CString str = CString(L"很好");
elem->setAttribute(bstr_t(L"name"), variant_t(str));
}*/elem 还是拿不到啊
pFrame 得到了没有?
还是说,pFrame->getElementById(CComBSTR(L"xixiking"), &elem);
这一步执行后elem为空?
这一步执行后elem为空
你把你frame页面的html代码贴出来
CComPtr<IHTMLDocument3> pDoc;
HRESULT hr = pDisp->QueryInterface(IID_IHTMLDocument3, (void**)&pDoc);
if (SUCCEEDED(hr))
{
hr = pDoc->getElementById(CComBSTR(szId), pElem);
return (bool)(SUCCEEDED(hr));
}
}上面的这段代码必现在页面的Loading结束之后才能获取到Element的。
感谢两位大虾!