mshtml的一个问题。我如果才能获得往head部分写一行html

chenzhou35 2002-03-02 04:29:22
我获得了head的IHTMLElementCollection,问题是我下一步该怎么从IHTMLElementCollection里正确的获取一个IHTMLElement呢?大家多多帮忙。

代码如下:
CString strPos = "BeforeBegin";
IHTMLElement *pElement= NULL;
IHTMLDocument2 *pDoc;
if(SUCCEEDED(HrGetDoc(&pDoc))) //获取IHTMLDocument2
{
IHTMLElementCollection *pElementCollection;
if(SUCCEEDED(pDoc->get_all(&pElementCollection)))
{
CString strname = "head";
VARIANT name,index;
VariantInit(&name);
VariantInit(&index);
V_VT(&name) = VT_BSTR ;
V_BSTR(&name) = strname.AllocSysString();
IHTMLElementCollection *pdisp;
if((pElementCollection->tags(name,(IDispatch**)&pdisp)) == S_OK)//获取head的IHTMLElementCollection
{
long len;
pdisp->get_length(&len);//运行到此处len为1
for(int i = 0;i<len;i++)
{
COleVariant varIndex,var2;
varIndex.vt=VT_UINT;
varIndex.lVal=i;
IHTMLElement *pElement;
if(pdisp->item(varIndex,var2,(IDispatch**)&pElement) == S_OK)
{
CString strScript = " <style type=text/css><!--p{margin-top: 0; margin-bottom: 0;}--></style>";
BSTR bstrPos = strPos.AllocSysString();
BSTR bstrContent = strScript.AllocSysString();
pElement->insertAdjacentHTML( bstrPos, bstrContent );//pElement不为空,但是出现非法操作,这里该怎么办呢?
::SysFreeString(bstrPos);
::SysFreeString(bstrContent);
pElement->Release();
}
}
pdisp->Release();
}
VariantClear(&name);
pElementCollection->Release();
}
pDoc->Release();
}
...全文
64 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenzhou35 2002-03-04
  • 打赏
  • 举报
回复
送分吧。
chenzhou35 2002-03-02
  • 打赏
  • 举报
回复
解决了。
IHTMLDocument2 *pDoc;
if(SUCCEEDED(HrGetDoc(&pDoc)))
{
CString strScript = "";
BSTR bstrContent = strScript.AllocSysString();
IHTMLStyleSheet* pnewStyleSheet;
if(pDoc->createStyleSheet(bstrContent,1,&pnewStyleSheet) == S_OK)
{
if(pnewStyleSheet != NULL)
{
CString strP = "p";
BSTR bstrP = strP.AllocSysString();
CString strStyle = "margin-top:0;margin-bottom:0";
BSTR bstrStyle = strStyle.AllocSysString();
long notuse;
pnewStyleSheet->addRule(bstrP,bstrStyle,-1,¬use);
::SysFreeString(bstrP);
::SysFreeString(bstrStyle);
pnewStyleSheet->Release();
}
}
::SysFreeString(bstrContent);
pDoc->Release();
}

谢谢。后天送分。
看看谁还有没有其他的方法。
某鸟 2002-03-02
  • 打赏
  • 举报
回复
收藏,呵呵.
chenzhou35 2002-03-02
  • 打赏
  • 举报
回复
没有关系,我的html全部都是自己生成的。是个封闭的系统。
111222 2002-03-02
  • 打赏
  • 举报
回复
你想想啊,要是我的HTML里没写<head>

你不是废了?
111222 2002-03-02
  • 打赏
  • 举报
回复
不用这么麻烦吧
干脆把整个HTML页的outerHtml村成CString
然后Find("<head>")之后加css
然后put_outerHTML回去

3,055

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC HTML/XML
社区管理员
  • HTML/XML社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧