douhuan1950 2012-02-17 16:56
浏览 47
已采纳

单击foreach循环显示/隐藏xml内容

I'm frustrated with this. What I am trying to code is a simple xml reader that allows user to choose which data is shown or hidden.

First this code shows the titles of the xml-file and when user clicks a title, date and time should be shown under the title.

The problem is that no matter which title is clicked, it only shows/hides the date-time of the first title under the first title.

<script type="text/javascript">
   function toggle(element) {
   document.getElementById(element).style.display =     
(document.getElementById(element).style.display == "none") ? "" : "none";
}
</script>

<?php
$doc = simplexml_load_file('data.xml');
if($doc == TRUE){
foreach($doc->channel->item as $child){
   $title = $child->title;
   echo '<table>';
   echo '<tr>';
   echo '<td><a href="javascript:toggle(\'shit\')">'.$title.'</a></td></tr>';
   echo '<tr id="shit" style="display: none;">';
   echo '<td>';
   echo date("d.m.Y H:i", strtotime($child->pubDate));
   echo '</td></tr>';
   echo '</table>';
}
}
?>
  • 写回答

3条回答 默认 最新

  • dongyuan2388 2012-02-17 16:59
    关注

    As the others said, you need to have a unique id per element (that you want to get a reference to). The simplest way is to just add a counter to the name you choose, in this case "shit", here's the modified version:

    <script type="text/javascript">
       function toggle(num) {
       document.getElementById("shit" + num).style.display =     
    (document.getElementById("shit" + num).style.display == "none") ? "" : "none";
    }
    </script>
    
    <?php
    $doc = simplexml_load_file('data.xml');
    if($doc == TRUE){
    $1 = 0;
    foreach($doc->channel->item as $child){
       $title = $child->title;
       echo '<table>';
       echo '<tr>';
       echo '<td><a href="javascript:toggle(\''.$i.'\')">'.$title.'</a></td></tr>';
       echo '<tr id="shit'.$i.'" style="display: none;">';
       echo '<td>';
       echo date("d.m.Y H:i", strtotime($child->pubDate));
       echo '</td></tr>';
       echo '</table>';
    }
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗