2012-02-17 61 views

回答

0

我试过这个和工作。

<?php 
    $dom = new DOMDocument('1.0', 'UTF-8'); 
    $dom->formatOutput = true; 
    $html = $dom->createElement('html'); 
    $dom->appendChild($html); 

    //head 
    $head = $dom->createElement('head'); 
    $title = $dom->createElement('title'); 
    $title->nodeValue = "title of my page"; 
    $head->appendChild($title); 
    $html->appendChild($head); 


    //body 
    $body = $dom->createElement('body'); 
    $h1 = $dom->createElement('h1'); 
    $h1->nodeValue = "Hello, World"; 
    $body->appendChild($h1); 
    $html->appendChild($body); 

    //form 
    $form = $dom->createElement('form'); 
    $form->setAttribute("action", ""); 
    $form->setAttribute("method", "post"); 

    $checkbox = $dom->createElement("input"); 
    $checkbox->setAttribute("type", "checkbox"); 
    $checkbox->setAttribute("name", "myCheckbox"); 
    $checkbox->setAttribute("value", "someval"); 
    $form->appendChild($checkbox); 

    $body->appendChild($form); 

    echo $dom->saveHTML(); 
?> 
+0

当然,你可以在脚下自己射击。我认为用这种OOP风格生成表示层是一种不好的做法 – 2012-07-11 09:31:55