幕布斯7119047
2022-05-18 19:15:37
例如:<root><p p1="1" p2="2"/></root>這樣的話,想通過js獲取p1或p2的值,改怎么寫。p能獲取到,但p1和p2不知道怎么獲取。
3 回答

汪汪一只貓
TA貢獻(xiàn)1898條經(jīng)驗 獲得超8個贊
給你個函數(shù),傳Node,屬性名進(jìn)去獲取屬性值,兼容所有瀏覽器:
function getAttributeValue (xmlNode,attrName){
if(!xmlNode)return "" ;
if(!xmlNode.attributes) return "" ;
if(xmlNode.attributes[attrName]!=null) return xmlNode.attributes[attrName].value ;
if(xmlNode.attributes.getNamedItem(attrName)!=null) return xmlNode.attributes.getNamedItem(attrName).value ;
return "" ;
}
獲取 p1 的值,就是 getAttributeValue(p,"p1") ;

墨色風(fēng)雨
TA貢獻(xiàn)1853條經(jīng)驗 獲得超6個贊
讀取到P的時候p.getAttribute("p1");p.getAttribute("p2");試試
或者你也可以寫成<root><p><p1>1</p1><p2>2</p2></p></root>
添加回答
舉報
0/150
提交
取消