我在我的Nodejs脚本中使用函数hasAttribute来检查它是否存在。
var DOMParser = require('xmldom').DOMParser;
var xmlDoc = new DOMParser().parseFromString(dataRegelWerk);
var allok = false;
var x = xmlDoc.getElementsByTagName("TCafe");
if (x.length != 0)
{
//Existiert Attribute prüfen
if(x.hasAttribute('initvalue') == true){
if(x.getAttribute('initvalue') == '1'){
if(x.hasAttribute('type') == true){
if(x.getAttribute('type') == 'int'){
allok = true;
}
}
}
}
}但是node一直告诉我hasAttribute不是一个函数。我对这条消息感到困惑,因为在所有的文档和引用中都有这个函数。我错过了什么吗?另一种方式?
发布于 2019-11-15 18:38:12
getElementsByTagName() 返回具有给定标记名的元素的实时HTMLCollection,您必须使用特定的索引。由于hasAttribute()返回布尔值,因此可以缩短条件:
if(x[0].hasAttribute('initvalue')){......和
if(x[0].getAttribute('initvalue') == '1'){.......https://stackoverflow.com/questions/58875170
复制相似问题