PHP使用DOMDocument类生成HTML实例(包含常见标签元(2)


$dom = new DOMDocument('1.0');
 
//CSS 内容
$css_text = '';
$css_text .= 'body{width:285px;margin:auto;margin-top:50px;}';
$css_text .= '#my_table{border:1px solid #ececec;}';
$css_text .= '#my_table th{border:1px solid #ececec;padding:5px;text-decoration:underline;}';
$css_text .= '#my_table td{border:1px solid #ececec;padding:5px;}';
$css_text .= '#my_table td:first-child{text-align:right;color:#333333;font-weight:bold;color:#999999;}';
 
//创建新的 style 标签和 CSS 内容
$style = $dom->createElement('style', $css_text);
 
//创建新的属性 'type'
$domAttribute = $dom->createAttribute('type');
 
//为属性 'type' 添加值
$domAttribute->value = 'text/css';
 
//添加该属性到 style 标签中
$style->appendChild($domAttribute);
 
//添加该 style 标签到 DOM 文件中
$dom->appendChild($style);
 
//添加 form
$form = $dom->createElement('form');
$dom->appendChild($form);
$formAttribute = $dom->createAttribute('method');
$formAttribute->value = 'post';
$form->appendChild($formAttribute);
 
//添加 table
$table = $dom->createElement('table');
$tableAttribute = $dom->createAttribute('id');
$tableAttribute->value = 'my_table';
$table->appendChild($tableAttribute);
 
//添加新的行(row)
$tr = $dom->createElement('tr');
$table->appendChild($tr);
 
//添加新的列(column)
$th = $dom->createElement('th', 'Generate HTML using PHP');
$tr->appendChild($th);
$thAttribute = $dom->createAttribute('colspan');
$thAttribute->value = '2';
$th->appendChild($thAttribute);
 
//添加新的行(row)
$tr = $dom->createElement('tr');
$table->appendChild($tr);
 
//添加新的列(column)
$td = $dom->createElement('td', 'First Name');
$tr->appendChild($td);
 
//添加新的列(column)
$td = $dom->createElement('td');
$tr->appendChild($td);
 
//添加 input 元素到列(column)中
$input = $dom->createElement('input');
$td->appendChild($input);
$tdAttribute = $dom->createAttribute('type');
$tdAttribute->value = 'text';
$input->appendChild($tdAttribute);
$tdAttribute = $dom->createAttribute('name');
$tdAttribute->value = 'f_name';
$input->appendChild($tdAttribute);
 
//添加新的行(row)
$tr = $dom->createElement('tr');
$table->appendChild($tr);
 
//添加新的列(column)
$td = $dom->createElement('td', 'Email');
$tr->appendChild($td);
 
//添加新的列(column)
$td = $dom->createElement('td');
$tr->appendChild($td);
 
//添加 input 元素到列(column)中
$input = $dom->createElement('input');
$td->appendChild($input);
$tdAttribute = $dom->createAttribute('type');
$tdAttribute->value = 'text';
$input->appendChild($tdAttribute);
$tdAttribute = $dom->createAttribute('name');
$tdAttribute->value = 'e-mail';
$input->appendChild($tdAttribute);
 
//添加新的行(row)
$tr = $dom->createElement('tr');
$table->appendChild($tr);
 
//添加新的列(column)
$td = $dom->createElement('td', 'Gender');
$tr->appendChild($td);
 
//添加新的列(column)
$td = $dom->createElement('td');
$tr->appendChild($td);
 
//添加 input 元素到列(column)中
$select = $dom->createElement('select');
$td->appendChild($select);
$tdAttribute = $dom->createAttribute('name');
$tdAttribute->value = 'gender';
$select->appendChild($tdAttribute);
 
//为 Select 下拉框添加选项
$opt = $dom->createElement('option', 'Male');
$domAttribute = $dom->createAttribute('value');
$domAttribute->value = 'male';
$opt->appendChild($domAttribute);
$select->appendChild($opt);
 
$opt = $dom->createElement('option', 'Female');
$domAttribute = $dom->createAttribute('value');
$domAttribute->value = 'female';
$opt->appendChild($domAttribute);
$select->appendChild($opt);
 
//添加新的行(row)
$tr = $dom->createElement('tr');
$table->appendChild($tr);
 
//添加新的列(column)
$td = $dom->createElement('td', 'Interest');
$tr->appendChild($td);
 
//添加新的列(column)
$td = $dom->createElement('td');
$tr->appendChild($td);
 
//添加 input 元素到列(column)中
$radio = $dom->createElement('input');
$td->appendChild($radio);
$radAttribute = $dom->createAttribute('type');
$radAttribute->value = 'radio';
$radio->appendChild($radAttribute);
$radAttribute = $dom->createAttribute('name');
$radAttribute->value = 'interest';
$radio->appendChild($radAttribute);
$radAttribute = $dom->createAttribute('id');
$radAttribute->value = 'php';
$radio->appendChild($radAttribute);
 
$label = $dom->createElement('label', 'PHP');
$labelAttribute = $dom->createAttribute('for');
$labelAttribute->value = 'php';
$label->appendChild($labelAttribute);
$td->appendChild($label);
 
$radio = $dom->createElement('input');
$td->appendChild($radio);
$radAttribute = $dom->createAttribute('type');
$radAttribute->value = 'radio';
$radio->appendChild($radAttribute);
$radAttribute = $dom->createAttribute('name');
$radAttribute->value = 'interest';
$radio->appendChild($radAttribute);
$radAttribute = $dom->createAttribute('id');
$radAttribute->value = 'jquery';
$radio->appendChild($radAttribute);
 
$label = $dom->createElement('label', 'jQuery');
$labelAttribute = $dom->createAttribute('for');
$labelAttribute->value = 'jquery';
$label->appendChild($labelAttribute);
$td->appendChild($label);
 
//添加新的行(row)
$tr = $dom->createElement('tr');
$table->appendChild($tr);
 
//添加新的列(column)
$td = $dom->createElement('td');
$tr->appendChild($td);
$tdAttribute = $dom->createAttribute('colspan');
$tdAttribute->value = '2';
$td->appendChild($tdAttribute);
 
//添加 input 元素到列(column)中
$input = $dom->createElement('input');
$td->appendChild($input);
$tdAttribute = $dom->createAttribute('type');
$tdAttribute->value = 'submit';
$input->appendChild($tdAttribute);
$tdAttribute = $dom->createAttribute('value');
$tdAttribute->value = 'Sign-Up';
$input->appendChild($tdAttribute);
 
//添加 table 到 form 中
$form->appendChild($table);
 
echo $dom->saveHTML();


您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/87fa2125c70149839032e78c828b811f.html