如何修改html标签属性
首先我的目的是:修改html中所有的img标签中的src属性为本地路径。
思路是通过IHTMLElementPtr浏览所有的img标签,然后进行属性设置
/////////////////////////////////////////////////////////
MSHTML::IHTMLElementPtr pElement;
MSHTML::IHTMLDocument2Ptr pDoc;
MSHTML::IHTMLDocument3Ptr pDoc3;
MSHTML::IHTMLElementCollectionPtr pCollection;
///省略一部分加载html文件的代码
pCollection = pDoc3->getElementsByTagName(L"img");
CString l_temp = (LPCTSTR)bstr_t(pElement->getAttribute(("id"), 2));
for(long i=0; i<pCollection->length; i++){
pElement = pCollection->item(i, (long)0);
//找到了该标签项,想要修改该src属性
pElement->setAttribute("src",CComVariant("c://test"),0);
}
在网上找到了IHTMLElement::setAttribute方法可以设置属性,按照上述方法使用IHTMLElementPtr试验了,没有成功,希望得到牛人解答。
[解决办法]
ie7以上版本需先将src设置成null,再设置成正确路径。
[解决办法]
可以试试 PutPropertyByName