DOMNodeList中的项目的反转顺序

编程入门 行业动态 更新时间:2024-10-16 02:24:53
本文介绍了DOMNodeList中的项目的反转顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

Hello 我正在制作RSS阅读器,并且正在使用DOM。 现在我卡住了,试图颠倒DOMNodeList中的项目顺序。 我可以使用2个循环 - 一个将其设置为数组,一个用于 rsort()。 有什么办法可以反转DOMNodeList中的顺序,还是必须完成数组方式?

Hello I'm making RSS reader and I'm using DOM. Now I stuck, trying to reverse the order of the items in the DOMNodeList. I can make it with 2 cycles - one to make it as array and one for rsort(). Is there any way to reverse the order in the DOMNodeList or must be done with "the array way"?

推荐答案

没有方法来反转DOMNodeList。

There is no method for reversing a DOMNodeList.

但是,您可以保持原样,如果需要,请从头开始执行。

But you can keep it as it is, and if you need it, walk through it from the end to the start.

示例:

<?php $doc=new DOMDocument; $doc->loadXML(' <div> <span>1 <span>2 <span>3 </span> </span> </span> </div>'); $nodeList=$doc->getElementsByTagName('span'); for($n=$nodeList->length-1;$n>=0;--$n) { echo $nodeList->item($n)->firstChild->data;//returns 321 } ?>

只需使用 NodeList->长度指向NodeList的末尾,然后递减索引并访问 NodeList-> item(index)

Just point at the end of the NodeList using NodeList->length, then decrement the index and access NodeList->item(index)

更多推荐

DOMNodeList中的项目的反转顺序

本文发布于:2023-11-03 18:47:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555812.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:顺序   项目   DOMNodeList

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!