doutu1939 2016-12-27 17:27
浏览 49

通过for循环插入php DOM元素

I am attempting to insert a new dom element after every 5 p tags but I cant get it to perform the insert. It is inserting at the end of the document. The page I am inserting into has 50+ p tags so I am not sure what I am doing wrong. Any help would be great. Here is what I am using:

$body = $this->get_body_node();

// Build our amp-ad tag
$ad_node = AMP_DOM_Utils::create_node( $this->dom, 'amp-ad', array(
    'width' => 300,
    'height' => 250,
    'type' => 'ad',
    'data-ad-client' => '###',
    'data-ad-slot' => '###'
) );

// Add a placeholder to show while loading
$fallback_node = AMP_DOM_Utils::create_node( $this->dom, 'amp-img', array(
    'placeholder' => '',
    'layout' => 'fill',
    'src' => 'https://placehold.it/300X250',
) );
$ad_node->appendChild( $fallback_node );

$p_nodes = $body->getElementsByTagName( 'p' );
$pListLength = $p_nodes->length;
if ( $pListLength > 5 ) {
    for($i=5; $i <= $pListLength; $i+=5) {
        $p_nodes->item($i)->parentNode->insertBefore( $ad_node, $p_nodes->item($i));
    }
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 公交车和无人机协同运输
    • ¥15 stm32代码移植没反应
    • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
    • ¥100 连续两帧图像高速减法
    • ¥15 组策略中的计算机配置策略无法下发
    • ¥15 如何绘制动力学系统的相图
    • ¥15 对接wps接口实现获取元数据
    • ¥20 给自己本科IT专业毕业的妹m找个实习工作
    • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
    • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)