doukunsan5553 2015-11-08 11:08
浏览 28
已采纳

DOM脚本getElementsByID - 链接

I'm learning php and DOM. And i have issue. So, I am trying to improve and understand the following. I have some links with ID div and I am attaching event on them:

<div  id='articleList1' >
    

            

            <div class="hotOne">
                <a class="" href="http://link1/index.html" >
                <span class="itemTitle">

        1 title
                </span>

                <img src="1.jpg" class=""     

                alt="1"
                title="1"
                    border="0"
                />
                       

                </a>
            </div>

            <div class="hotThree">
                <ul>
             
 <li class="item item1 ">
         
     <a href="http://link2/index.html" title="2" >
                    <span class="itemTitle"> 
 2 title
                    </span>        <img src="2.jpg" class=""     

                alt="2"
                title="2"
                    border="0"
                />        <p>             2 tekst </span>         </p>     </a>
                                                                               
 </li>             
 <li class="item item2 ">
         
     <a href="http://link3.html" title="3" >
                    <span class="itemTitle"> 
 3 title
                    </span>        <img src="3.jpg" class=""     

                alt="3"
                title="3"
                    border="0"
                />        <p>             3 tekst </span>         </p>     </a>
                                                                               
 </li>
                        
</div>

<?php $div1 = $dom->getElementById('articleList1');
 $href = $div1->getElementsByTagName('a');


 foreach ($href as $hrefs) {
  $link = $hrefs->getAttribute('href');
}

echo '<a href ="'.$link.'">'.$link.'<br>'); ?>

However, this script does not work. And I don't know why it's not work?. Thx for all answers.

</div>
  • 写回答

2条回答 默认 最新

  • doupeng8494 2015-11-08 11:19
    关注

    Your HTML markup is pretty poor - there are mistakes creeping in which will not help working with the DOM.

    <div  id='articleList1' >
        <div class="hotOne">
            <a class="" href="http://link1/index.html" >
                <span class="itemTitle">1 title</span>
                <img src="1.jpg" class="" alt="1" title="1" border="0" />
            </a>
        </div>
        <div class="hotThree">
            <ul>
                <li class="item item1 ">
                    <a href="http://link2/index.html" title="2" >
                        <span class="itemTitle">2 title</span>
                        <img src="2.jpg" class="" alt="2" title="2" border="0" />
                        <p>2 tekst</p><!-- <~~~ error here previously -->
                    </a>
                </li>
                <li class="item item2 ">
                    <a href="http://link3.html" title="3" >
                        <span class="itemTitle">3 title</span>
                        <img src="3.jpg" class="" alt="3" title="3" border="0" />
                        <p>3 tekst</p><!-- <~~~ error here previously -->
                    </a>
                </li>
            </ul><!-- missing -->
        </div>
    </div><!-- missing -->
    
    
    <?php
        $col=$dom->getElementsByTagName('a');
        foreach( $col as $node ){
            if( $node->nodeType===XML_ELEMENT_NODE && $node->hasAttribute('href') ){
                echo $node->getAttribute('href');
            }
        }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错