通过IHTMLDocument2和C ++更改div innerHTML

时间:2017-09-19 13:23:42

标签: c++ ihtmldocument2 ihtmldocument

我试图以这种方式使用IHTMLDocument2界面更改div的内容:

    IHTMLElementCollection* collection = NULL;
    IDispatch* mydiv;

    doc2->get_all(&collection);
    long count;
    collection->get_length(&count);     //just to check I get something

    CComVariant varstr = L"mydivname";
    CComVariant varint = 0;
    collection->item(varstr, varint, &mydiv);    //this works I get the div
    IHTMLElement* htmldiv;
    mydiv->QueryInterface(IID_IHTMLElement, (void**)&htmldiv);

    CComBSTR html;
    htmldiv->get_innerHTML(&html);      //works too, I get the current content

    HRESULT hr=htmldiv->put_innerText(L"hello");      //this does not work but returns S_OK

    collection->Release();

所以我的div的内容刚刚被删除,而不是用"你好",我不明白为什么,这可能是一个安全问题吗?

由于

1 个答案:

答案 0 :(得分:0)

根据MSDN documentation,传递给put_innerText的字符串属于BSTR类型。

所以,我建议尝试这样的代码:

CComBSTR text(OLESTR("hello"));
hr = htmldiv->put_innerText(text);