【问题标题】:how to add another html tag如何添加另一个html标签
【发布时间】:2017-02-28 03:51:11
【问题描述】:
$iframes = $doc->getElementsByTagName('iframe');
foreach ($iframes as $iframeViejo) {
   $iframeMainn = $doc->createElement('iframe');
   $iframeNuevo->setAttribute('src', $iframeViejo->getAttribute('src'));
   $iframeNuevo->setAttribute('width','560');
   $iframeNuevo->setAttribute('height','615');
   $figureNuevo = $doc->createElement('figure');
   $figureNuevo->setAttribute('class','op-interactive');
   $figureNuevo->appendChild($iframeNuevo);
   $iframeViejo->parentNode->replaceChild($figureNuevo, $iframeViejo);
}

但我想添加另一个 iframe 标记,因为我想要这个输出:

<figure class="class"><iframe><iframe src="src" width="xxx" height="xxx"></iframe><iframe></figure>

你能帮帮我吗

【问题讨论】:

  • 通过 for 循环添加 iframe,然后单独添加?这是你要求的吗?
  • 是的,我需要添加另一个 iframe
  • $iframeNuevo 是什么对象

标签: php html dom tags


【解决方案1】:

这里是JavaScript代码,你可以试试

function CreateTag(TAG){
    return document.createElement(TAG);
}

var iframes = document.getElementsByTagName('iframe');
var totalIframe = iframes.length;

for(var i=0; i<totalIframe; i++){
    figure = CreateTag('figure');  /*Create <figure> Tag*/
    iframe = CreateTag('iframe');  /*Create <iframe> Tag*/

    figure.setAttribute('class','ClassName');  /*Set class="ClassName"*/

    iframe.setAttribute('src',iframes[i].src); /*Set main iframe src to new <iframe> src*/
    iframe.setAttribute('width','200');  /*Set new <iframe> width*/
    iframe.setAttribute('height','100'); /*Set new <iframe> height*/

    figure.appendChild(iframe);     /*Append new <figure> to <figure> Tag*/
    iframes[i].replaceWith(figure); /*Replace main <iframe> with new <figure> Tag*/
}

注意:以上代码输出如下

<figure class="ClassName"><iframe src="{main-iframe-src}" width="200" height="100"><iframe></figure>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-22
    • 2013-04-23
    • 2022-01-09
    相关资源
    最近更新 更多