javascript - 在 DomParser 中使用 hasAttribute

标签 javascript node.js xml domparser

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

相关文章:

javascript - 类型错误 : Cannot read properties of undefined when sending an EJS form to Express

javascript - "var links = [].slice.apply(document.getElementsByTagName(' a'));"真的是做什么的?

javascript - AngularJS 智能表全局配置实现

javascript - Material-UI TreeView TreeItems 背景

javascript - jQuery 完整日历事件标题时间错误

c# - 如何限制 asp.net 中 XML 提要中显示的记录数?

ajax - 为什么在 WebSockets 可用时使用 AJAX?

mysql - 使用node.js缓存mysql数据库

sql - 如何从一节中的sql数据创建xml

xml - 引用节点值时包含 ColdFusion 11 xml 标记