gpt4 book ai didi

javascript - 在 DomParser 中使用 hasAttribute

转载 作者:行者123 更新时间:2023-11-30 19:07:01 26 4
gpt4 key购买 nike

我在我的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com