使用DOMDocument在特定位置的HTML文件中添加元素

时间:2014-08-15 21:45:02

标签: javascript php jquery html html5

我的php是:

  <?php 
         $Obj = $_POST['OGGETTO']; 
         $Descr = $_POST['DESCRIZIONE']; 


            $doc = new DOMDocument;
            $doc->loadHtmlFile("index.html");

            // Get the parent node where you want the insertion to occur
            $parent = $doc->getElementsByTagName('div');

            $after = "$$$$"; // i know it's wrong

            // Append (insert) the child to the parent node
            $parent->appendChild($after);
            // Save the resulting HTML
            echo $doc->saveHTML();      
     ?>

我的HTML文件(index.html)

 <body>
    ......
    ......
        <div class="one">

                [**NEW DIV HERE**]

        </div>
    </body>

我想要完成的是:我的php脚本应该在之前的[ NEW DIV HERE ]中添加以下标记并保存“index.html”文件。而且“[ OGGETTO ]”值应替换为“$ Obj”和“[ DESCRIZIONE ]”替换为“$ Derc”

DIV放入HTML文件:

<div class="two">
        <div class="three">
            <a>
                <img src="images/pic2.jpg" alt="" />
                <span class="next"> </span>
            </a>
        </div>
        <h4><a href="#"><span>[**OGGETTO**]</span></a></h4>
        <p>[**DESCRIZIONE**]</p>
    </div>

因此,如果$ Obj =“MENU”且$ Desrc =“这是一个网站”,那么最终的index.html文件将是

<body>
       <div class="one">

          <div class="two">
            <div class="three">
                <a>
                    <img src="images/pic2.jpg" alt="" />
                    <span class="next"> </span>
                </a>
            </div>
            <h4><a href="#"><span>MENU</span></a></h4>
            <p>this is a website</p>
        </div>

       </div>
</body>

0 个答案:

没有答案