selectChildByAttrbuteValue(attribute, value)
{
if(this.childNodes.length!=0)
{
for(var i=0; i<this.childNodes.length; i++)
{
if(typeof this.childNodes[i].attributes[attribute]!="undefined")
{
if(this.childNodes[i].attributes[attribute]==value)
{
return this.childNodes[i];
}else
{
this.childNodes[i].selectChildByAttrbuteValue(attribute, value);
}
}else
{
this.childNodes[i].selectChildByAttrbuteValue(attribute, value);
}
}
}
}
此代码返回undefined,而当我执行console.log(this.childNodes [i]);记录我的对象。然后我应该返回..但它返回undefined!
全班
class Node
{
constructor(nodeName, nodeType)
{
this.nodeName = nodeName;
this.nodeType = nodeType;
this.attributes = {};
this.childNodes = [];
this.parentNode = null;
}
removeChild(node)
{
if(node.parentNode!=null)
{
for(var i=0; i<this.childNodes.length; i++)
{
if(node == this.childNodes[i])
{
this.childNodes.splice(i, 1);
node.parentNode = null;
}
}
}
}
appendChild(child)
{
if(child.parentNode==null)
{
this.childNodes.push(child);
child.parentNode = this;
}else
{
child.parentNode.removeChild(child);
this.childNodes.push(child);
child.parentNode = this;
}
}
selectChildByAttrbuteValue(attribute, value)
{
if(this.childNodes.length!=0)
{
for(var i=0; i<this.childNodes.length; i++)
{
if(typeof this.childNodes[i].attributes[attribute]!="undefined")
{
if(this.childNodes[i].attributes[attribute]==value)
{
return this.childNodes[i];
}else
{
this.childNodes[i].selectChildByAttrbuteValue(attribute, value);
}
}else
{
this.childNodes[i].selectChildByAttrbuteValue(attribute, value);
}
}
}
}
}
一个完整的课程,看到ghow正好循环。
Xml_Node {
nodeName: 'row',
nodeType: 'XML_ELEMENT',
attributes: { name: 'Lee Pellion', characterID: '93746314' },
childNodes: [],
parentNode:
Xml_Node {
nodeName: 'rowset',
nodeType: 'XML_ELEMENT',
attributes:
{ name: 'characters',
key: 'characterID',
columns: 'name,characterID' },
childNodes: [ [Circular] ],
parentNode:
Xml_Node {
nodeName: 'result',
nodeType: 'XML_ELEMENT',
attributes: {},
childNodes: [Object],
parentNode: [Object],
innerText: '' },
innerText: '' },
innerText: '' }
根对象
Xml_Node {
nodeName: 'root',
nodeType: 'XML_ELEMENT',
attributes: {},
childNodes:
[ Xml_Node {
nodeName: 'currentTime',
nodeType: 'XML_ELEMENT',
attributes: {},
childNodes: [],
parentNode: [Circular],
innerText: '2016-12-06 01:20:09' },
Xml_Node {
nodeName: 'result',
nodeType: 'XML_ELEMENT',
attributes: {},
childNodes: [Object],
parentNode: [Circular],
innerText: '' },
Xml_Node {
nodeName: 'cachedUntil',
nodeType: 'XML_ELEMENT',
attributes: {},
childNodes: [],
parentNode: [Circular],
innerText: '2017-01-06 01:20:09' } ],
parentNode: null,
innerText: '' }
使用控制台日志它可以工作,但然后对象只是在某处消失了!
selectChildByAttributeValue(attribute, value) {
if (this.childNodes.length != 0) {
for (var i = 0; i < this.childNodes.length; i++) {
if (typeof this.childNodes[i].attributes[attribute] != 'undefined') {
if (this.childNodes[i].attributes[attribute] == value) {
console.log(this.childNodes[i]);
return this.childNodes[i];
} else {
return this.childNodes[i].selectChildByAttributeValue(attribute, value);
}
} else {
return this.childNodes[i].selectChildByAttributeValue(attribute, value);
}
}
}
答案 0 :(得分:0)
正如我们在评论中所说,您已向我提供了更多信息,我想出了以下内容:
该功能应如下:
function selectChildByAttributeValue(attribute, value) {
if (this.childNodes.length != 0) {
for (var i = 0; i < this.childNodes.length; i++) {
if (typeof this.childNodes[i].attributes[attribute] != 'undefined') {
if (this.childNodes[i].attributes[attribute] == value) {
return this.childNodes[i];
} else {
return this.childNodes[i].selectChildByAttributeValue(attribute, value);
}
} else {
return this.childNodes[i].selectChildByAttributeValue(attribute, value);
}
}
}
}
使用return语句,因此它可以正确返回嵌套的子级。
您的主节点没有任何子节点,这就是它返回undefined
的原因。
我在终端中做了以下操作,然后我找回了正确的节点。
const n = new Node('row', 'XML_ELEMENT');
n.appendChild(new Node('rowset', 'XML_ELEMENT'));
n.appendChild(new Node('rowset', 'XML_ELEMENT'));
n.childNodes[0].attributes = {name: 'characters', key: 'characterID', columnds: 'name, characterID'};
n.selectChildByAttributeValue('name', 'characters');
具有以下结果:
Node {
nodeName: 'rowset',
nodeType: 'XML_ELEMENT',
attributes:
{ name: 'characters',
key: 'characterID',
columnds: 'name, characterID' },
childNodes: [],
parentNode:
Node {
nodeName: 'row',
nodeType: 'XML_ELEMENT',
attributes: {},
childNodes: [ [Circular], [Object] ],
parentNode: null } }
我的根对象(在上面声明为n
)如下:
Node {
nodeName: 'row',
nodeType: 'XML_ELEMENT',
attributes: {},
childNodes:
[ Node {
nodeName: 'rowset',
nodeType: 'XML_ELEMENT',
attributes: [Object],
childNodes: [],
parentNode: [Circular] },
Node {
nodeName: 'rowset',
nodeType: 'XML_ELEMENT',
attributes: {},
childNodes: [],
parentNode: [Circular] } ],
parentNode: null }
你的函数循环自己找东西,但只有1个return语句 因此,当它调用自己查看子值并返回某些内容时,它不会存储在变量中或返回,因此它无效。
您可以通过在嵌套调用之前放置return
来解决此问题
所以this.childNodes[i].selectChildByAttrbuteValue(attribute, value);
都应该是return this.childNodes[i].selectChildByAttrbuteValue(attribute, value);
。
此外,没有默认值,因此,如果您要查找某个值,但没有一个属性保留该值,那么您还会获得undefined
。
小注意:您目前允许在null
语句中接受值if
,这可能是不受欢迎的行为。