2

我正在尝试以这种方式使用 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 的内容只是被清除而不是替换为“你好”,我不明白为什么,这可能是一个安全问题吗?

谢谢

4

1 回答 1

0

根据MSDN 文档,传递给的字符串put_innerText是类型BSTR

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

CComBSTR text(OLESTR("hello"));
hr = htmldiv->put_innerText(text);
于 2017-09-19T13:32:06.387 回答