Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 查找DOM节点';s属性(更具体地说,是div';s类)_Javascript_Dom_Attributes - Fatal编程技术网

Javascript 查找DOM节点';s属性(更具体地说,是div';s类)

Javascript 查找DOM节点';s属性(更具体地说,是div';s类),javascript,dom,attributes,Javascript,Dom,Attributes,这是我的密码: <body class="asdf"> <span>hey! <div class='hideContent'>test</div> the ultimate experience</span> <span id="blarg">original</span> </body> <script> function pullE

这是我的密码:

<body class="asdf">
        <span>hey! <div class='hideContent'>test</div> the ultimate experience</span>
        <span id="blarg">original</span>
</body>

<script>

        function pullElementsOut(searchClass, searchNode) {
                var childNodes = (searchNode || document.body).childNodes, cnLength = childNodes.length;
                var excludes = 'html,head,style,title,link,meta,script,object,iframe';
                while (cnLength--) {
                        var currentNode = childNodes[cnLength];
                        alert(currentNode.nodeType+" "+currentNode.localName + " " + currentNode.hasAttributes());
                        if (currentNode.nodeType === 1 && (excludes + ',').indexOf(currentNode.nodeName.toLowerCase() + ',') === -1) {
                                arguments.callee(searchClass, currentNode);
                        }
                        if (currentNode.nodeType !== 2) {
                                continue;
                        }
                }
        }
        pullElementsOut('hideContent');
</script>

嘿测试终极体验
起初的
函数PullerElementSout(searchClass,searchNode){
var childNodes=(searchNode | | document.body).childNodes,cnLength=childNodes.length;
var excludes='html、head、style、title、link、meta、script、object、iframe';
while(cnLength--){
var currentNode=childNodes[cnLength];
警报(currentNode.nodeType+“”+currentNode.localName+“”+currentNode.hasAttributes());
if(currentNode.nodeType==1&&(排除+',')).indexOf(currentNode.nodeName.toLowerCase()+',')==1){
arguments.callee(searchClass,currentNode);
}
如果(currentNode.nodeType!==2){
持续
}
}
}
拉勒门特(hideContent);
如您所见,我有一个未完成的函数pullElementsOut。我想确定该div的“hideContent”类。我的最终目标是提取所有元素并删除现在为空的div。但是,我似乎不知道如何访问节点的类属性,更不用说修改它了


有什么帮助吗?

不确定,但我认为可以使用
getAttribute('class')
方法来完成您想要的。

您想要元素的
className
属性看看jQuery。在那里,您可以使用css字符串来查找dom元素,例如,
$(“div.hideContent”)
不能使用jquery。尝试制作超可移植代码:(另外,我刚刚了解到节点和元素是同一件事。见鬼,为什么同一件事有两个不同的名称?是一种类型。DOM中的所有内容都是节点,有些节点是元素。使用
className
DOM属性,这是最简单和最兼容的访问方法。