反转 DOMNodeList 中项目的顺序

发布于 2024-09-25 08:34:37 字数 177 浏览 2 评论 0原文

你好 我正在制作 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"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

哽咽笑 2024-10-02 08:34:37

没有方法可以反转 DOMNodeList。

但您可以保持原样,如果需要,请从头到尾浏览一遍。

示例:

<?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->length 指向 NodeList 的末尾,然后递减索引并访问 NodeList->item(index)

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.

Example:

<?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
}
?>

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

短叹 2024-10-02 08:34:37

使用 documentFragment 的替代方法(最终可能会出现不需要的“default:”命名空间前缀):克隆 NodeList,将所有项目传输到克隆,然后替换原始节点:

function reverseNodeList($nodeList) {
    // clone the original node
    $reverseNodeList = $nodeList->cloneNode();
    // move all nodes off the bottom of the original list onto the new one
    while ($nodeList->lastChild) $reverseNodeList->appendChild($nodeList->lastChild);
    // replace the original node with the new one
    $nodeList->parentNode->replaceChild($reverseNodeList, $nodeList);
}

An alternative to using a documentFragment (which can end up with an unwanted "default:" namespace prefix): clone the NodeList, transfer all the items to the clone, then replace the original node:

function reverseNodeList($nodeList) {
    // clone the original node
    $reverseNodeList = $nodeList->cloneNode();
    // move all nodes off the bottom of the original list onto the new one
    while ($nodeList->lastChild) $reverseNodeList->appendChild($nodeList->lastChild);
    // replace the original node with the new one
    $nodeList->parentNode->replaceChild($reverseNodeList, $nodeList);
}
幸福丶如此 2024-10-02 08:34:37

为什么不在客户端使用 javascript 来完成呢?
给定节点 n 的代码为:

function reverse(n) {  // Reverses the order of the children of Node n
    var f = document.createDocumentFragment(  );  // Get an empty DocumentFragment
    while(n.lastChild)                 // Loop backward through the children,
          f.appendChild(n.lastChild);  // moving each one to the DocumentFragment
    n.appendChild(f);                  // Then move them back (in their new order)
}

Why don't you do it on the client side using javascript?
The code for a given node n would be:

function reverse(n) {  // Reverses the order of the children of Node n
    var f = document.createDocumentFragment(  );  // Get an empty DocumentFragment
    while(n.lastChild)                 // Loop backward through the children,
          f.appendChild(n.lastChild);  // moving each one to the DocumentFragment
    n.appendChild(f);                  // Then move them back (in their new order)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文