DOMNodeList中的项目的反转顺序 [英] Reversing order of items in DOMNodeList

查看:115
本文介绍了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中的项目的反转顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆