我在我的 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 不是一个函数。我对此消息感到困惑,因为在所有文档和引用资料中都可以使用此功能。我想念什么吗? 替代方法?
最佳答案
getElementsByTagName()
返回具有给定标签名称的元素的实时 HTMLCollection,您必须使用特定的索引。由于 hasAttribute()
返回 bool 值,您可以缩短条件:
if(x[0].hasAttribute('initvalue')){......
和
if(x[0].getAttribute('initvalue') == '1'){.......
关于javascript - 在 DomParser 中使用 hasAttribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58875170/