我正在使用IHTMLdocument2来创建Element
这是我的代码:
IHTMLdocument2 pDoc2;//it is initialized in ma code
BSTR eTag = SysAllocString(L"TR");
IHTMLElement *pTRElmt = NULL;
hr = pDoc2->createElement(eTag,&pTRElmt);
if(FAILED(hr))
return hr;
IHTMLDOMNode *pTRNode = NULL;
hr = pTRElmt->QueryInterface(IID_IHTMLDOMNode, (void **)&pTRNode);
if(FAILED(hr))
return hr;
// create TD node
IHTMLElement *pTDElmt = NULL;
hr = pDoc2->createElement(L"TD",&pTDElmt);
if(FAILED(hr))
return hr;
IHTMLDOMNode *pTDNode = NULL;
hr = pTDElmt->QueryInterface(IID_IHTMLDOMNode,(void **)&pTDNode);
if(FAILED(hr))
return hr;
IHTMLDOMNode *pRefNode = NULL;
hr = pTRNode->appendChild(pTDNode,&pRefNode);
if(FAILED(hr))
return hr;
// create TEXT Node
IHTMLDOMNode *pTextNode = NULL;
hr = pDoc3->createTextNode(L"madhu", &pTextNode);
if(FAILED(hr))
return hr;
IHTMLDOMNode *pRefNod = NULL;
hr = pTDNode->appendChild(pTextNode,&pRefNod);
if(FAILED(hr))
return hr;
//********* setting attributes for <tr>
/*
VARIANT bgclor;
bgclor.vt = VT_I4;
bgclor.lVal =0xC0C0C0;
hr = newElem->setAttribute(L"bgcolor",bgclor,1);
if(FAILED(hr))
return hr;
VARIANT style;
style.vt = VT_BSTR;
style.bstrVal = SysAllocString(L"display: table-row");
hr = newElem->setAttribute(L"style",style,1);
if(FAILED(hr))
return hr;
VARIANT id;
id.vt = VT_BSTR;
id.bstrVal = SysAllocString(L"AttrRowMiddleName");
hr = newElem->setAttribute(L"id",id,1);
if(FAILED(hr))
return hr;
*/
//create <td> for row <tr>
/*
VARIANT Name;
Name.vt = VT_BSTR;
Name.bstrVal = SysAllocString(L"MiddleName");
hr = newElem->setAttribute(L"name",Name,1);
if(FAILED(hr))
return hr;
VARIANT Type;
Type.vt = VT_BSTR;
Type.bstrVal = SysAllocString(L"text");
hr = newElem->setAttribute(L"type",Type,1);
if(FAILED(hr))
return hr;
VARIANT Value;
Value.vt = VT_BSTR;
Value.bstrVal = SysAllocString(L"button");
hr = newElem->setAttribute(L"value",Value,1);
if(FAILED(hr))
return hr;
*/
//IHTMLDOMNode *pReturn = NULL;
//hr = pParentNode->replaceChild(pdn,pFirstchild,&pReturn);
//if(FAILED(hr))
// return hr;
VARIANT refNode;
refNode.vt = VT_DISPATCH;
refNode.pdispVal = pDomNode;
IHTMLDOMNode *pREfTochild = NULL;
hr = pParentNode->insertBefore(pTRNode,refNode,&pREfTochild);
if(FAILED(hr))
return hr;
这是插入一些但不可见的插入,当tr标记到来时插入
我甚至试过克隆但同样的问题。
请任何人为此提供正确的代码
答案 0 :(得分:2)
不确定仍然需要这个,但这是我如何解决问题。如果你查看代码示例,我的代码剪切和madhu之间没有太大的变化。
诀窍在于如何传递“TR”字符串。我无法弄清楚为什么它很重要,但确实如此。也许其他人可以解释为什么。
这不起作用:
BSTR eTag = SysAllocString(L"TR");
hr = pDoc2->createElement(eTag,&pTRElmt);
这样做:
hr = pDoc2->createElement(L"TR",&pTRElmt);
以下代码剪辑对我有用:
// create table node
IHTMLElement *pTableElmt = NULL;
hr = pDoc2->createElement(L"TABLE",&pTableElmt);
if(FAILED(hr))
return hr;
IHTMLDOMNode *pTableNode = NULL;
hr = pTableElmt->QueryInterface(IID_IHTMLDOMNode, (void **)&pTableNode);
if(FAILED(hr))
return hr;
// create TR node
BSTR eTag = SysAllocString(L"TR");
IHTMLElement *pTRElmt = NULL;
hr = pDoc2->createElement(L"TR",&pTRElmt);
if(FAILED(hr))
return hr;
IHTMLDOMNode *pTRNode = NULL;
hr = pTRElmt->QueryInterface(IID_IHTMLDOMNode, (void **)&pTRNode);
if(FAILED(hr))
return hr;
IHTMLDOMNode *pRefNode = NULL;
hr = pTableNode->appendChild(pTRNode,&pRefNode);
if(FAILED(hr))
return hr;
// create TD node
IHTMLElement *pTDElmt = NULL;
hr = pDoc2->createElement(L"TD",&pTDElmt);
if(FAILED(hr))
return hr;
IHTMLDOMNode *pTDNode = NULL;
hr = pTDElmt->QueryInterface(IID_IHTMLDOMNode,(void **)&pTDNode);
if(FAILED(hr))
return hr;
hr = pTRNode->appendChild(pTDNode,&pRefNode);
if(FAILED(hr))
return hr;
// create TEXT Node
IHTMLDOMNode *pTextNode = NULL;
hr = pDoc3->createTextNode(L"madhu", &pTextNode);
if(FAILED(hr))
return hr;
hr = pTDNode->appendChild(pTextNode,&pRefNode);
if(FAILED(hr))
return hr;
//add to body
CComQIPtr<IHTMLElement> pBody;
CComQIPtr<IHTMLDOMNode> pBodyNode;
pDoc2->get_body( &pBody );
pBody->QueryInterface(IID_IHTMLDOMNode, (void**)&pBodyNode);
HRESULT hr = pBodyNode->appendChild( pTableNode, &pRefNode);