【问题标题】:Change div innerHTML through IHTMLDocument2 and C++通过 IHTMLDocument2 和 C++ 更改 div innerHTML
【发布时间】:2017-09-19 13:23:41
【问题描述】:

我正在尝试以这种方式使用 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 的内容只是被清除了,并没有被替换为“hello”,我不明白为什么,会不会是安全问题?

谢谢

【问题讨论】:

    标签: c++ ihtmldocument2 ihtmldocument


    【解决方案1】:

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

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

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

    【讨论】:

    • 没问题。很高兴有帮助。这里的“棘手”部分是,当我们传递普通的 wchar_t 字符串而不是 BSTR 字符串时,我们不会出现编译时错误(考虑到 BSTR 的 typedef 定义,这从编译器的角度来看是有意义的:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多