dongtan4046 2016-08-15 18:20
浏览 50
已采纳

PHP排序DOM元素和替换

I currently have a ul list that looks like this:

<ul id="primary-nav">
    <li class="navButton" id="amps_off_button" data-order="6">
        <span class="navButton-color red"><p>AMPS Off</p></span>
        <span class="glare"></span>
    </li>
    <li class="navButton" id="amps_alarms" data-order="7">
        <span class="navButton-color"><p>AMPS Alarms</p></span>
        <span class="glare"></span>
    </li>
    <li class="navButton" id="sysConfig" data-order="3">
        <span class="navButton-color"><p>System Configuration</p></span>
        <span class="glare"></span></li>
    <li class="navButton" id="system" data-order="4">
        <span class="navButton-color"><p>System</p></span>
        <span class="glare"></span>
    </li>
    <li class="navButton" id="backBtn" data-order="100">
        <span class="navButton-color"><p>BACK</p></span>
        <span class="glare"></span>
    </li>
    <li class="navButton" id="rescan" data-order="101">
        <span class="navButton-color"><p>ReScan</p></span>
        <span class="glare"></span>
    </li>
</ul>

In my Navigation class, I have a function that can be called that will sort the order of the unordered list by the value in data-order:

public function setOrder() {
    $liNodeList = $this->xpath->query("//ul[@id='primary-nav']//li");
    $liNodes = iterator_to_array($liNodeList);

    usort($liNodes, array('Navigation', 'sortListByOrderAttr'));

    if ($liNodeList->length == count($liNodes)) {
        for ($i = 0; $i < $liNodeList->length; $i++) {
            $node = $liNodeList->item($i);
            $newNode = $liNodes[$i];
            $node->parentNode->replaceChild($newNode, $node);
        }
    }
    else echo "error";
}

private static function sortListByOrderAttr($a, $b) {
    return (int) $a->getAttribute('data-order') - (int) $b->getAttribute('data-order');
}

For some reason, I am not getting the correct order of the list. Instead, the output shows 4 items instead of the 6 items it's supposed to show:

<ul id="primary-nav">
    <li class="navButton" id="amps_off_button" data-order="6">
        <span class="navButton-color red"><p>AMPS Off</p></span>
        <span class="glare"></span>
    </li>
    <li class="navButton" id="amps_alarms" data-order="7">
        <span class="navButton-color"><p>AMPS Alarms</p></span>
        <span class="glare"></span>
    </li>
    <li class="navButton" id="backBtn" data-order="100">
        <span class="navButton-color"><p>BACK</p></span>
        <span class="glare"></span>
    </li>
    <li class="navButton" id="rescan" data-order="101">
        <span class="navButton-color"><p>ReScan</p></span>
        <span class="glare"></span>
    </li>
</ul>

I suspect my problem here is that my $node->parentNode->replaceChild($newNode, $node); is no es bueno. Maybe I need a different approach to sort this? Help appreciated!

  • 写回答

1条回答 默认 最新

  • 普通网友 2016-08-15 19:48
    关注

    Well, after thinking about what cHao was saying, it appears that after re-reading how replaceChild works, it explains why my method didn't yield the correct result, due to the new child nodes already existed in the node set.

    I've fixed the code as such:

    public function setOrder() {
        $liNodeList = $this->xpath->query("//ul[@id='primary-nav']//li");
        $liNodes = iterator_to_array($liNodeList);
    
        usort($liNodes, array('Navigation', 'sortListByOrderAttr'));
    
        if ($liNodeList->length == count($liNodes)) {
            for ($i = 0; $i < $liNodeList->length; $i++) {
                $node = $liNodeList->item($i);
                $newNode = $this->doc->importNode($liNodes[$i], TRUE);
                $node->parentNode->appendChild($newNode);
            }
        }
        else echo "error with nav menu";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?