我的xml文檔是這樣的:爲什麼設置innerHTML不起作用?
<Order>
<size width="16.5" height="19.5">
<width>16.5</width>
<height>19.5</height>
</size>
<type>photo</type>
</overlay>
<Mats></Mats>
<Openings></Openings>
<Moulding></Moulding>
<Glass></Glass>
</Order>
我試圖填補墊變量的值。 這裏是我的javascript代碼:
function common_get_order_xml()
{
var parser = new DOMParser(); //create a new DOMParser
var doc = parser.parseFromString(common_get_common_order_xml(), "application/xml"); //convert the string to xml
console.log(doc); //outputs what is above
//doc.getElementsByTagName('Mats').innerHTML = mattes_get_mattes_xml().replace("<Mats>", "").replace("</Mats>", ""); //add the mattes
doc.getElementsByTagName('Mats').innerHTML = "TEST";
doc.getElementsByTagName('Openings').innerHTML = mattes_get_openings_xml(); //Add the openings
doc.getElementsByTagName('Moulding').innerHTML = moulding_get_moulding_xml(); //Add the moulding
doc.getElementsByTagName('Glass').innerHTML = glazing_get_glass_xml(); //Add the moulding
var serializer = new XMLSerializer(); //create a new XMLSerializer
common_order_xml = serializer.serializeToString(doc); //convert the xml back to a string
return doc;
}
但是當我輸出的文檔,該墊的innerHTML不被設置爲「TEST」
[* getElementsByTagName *](http://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-A6C9094)返回[* NodeList *](http:// www .w3.org/TR/DOM-Level-3-Core/core.html#ID-536297177),而不是一個單一的元素。試試'doc.getElementsByTagName('Mats')[0] .innerHTML'。 – RobG
@RobG是正確的......你的XML格式也很糟糕。關閉''沒有開標籤。它應該是一個自閉標籤嗎?在這種情況下,請使用' ' –
freefaller
@RobG。如果你把它作爲答案,我會接受它。 – AllisonC