六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 155|回复: 0

动态创建FORM和表格

[复制链接]

升级  18.33%

75

主题

75

主题

75

主题

举人

Rank: 3Rank: 3

积分
255
 楼主| 发表于 2013-2-7 20:48:39 | 显示全部楼层 |阅读模式
<script>//helper function to create the formfunction getNewSubmitForm(){ var submitForm = document.createElement("FORM"); document.body.appendChild(submitForm); submitForm.method = "POST"; return submitForm;}//helper function to add elements to the formfunction createNewFormElement(inputForm, elementName, elementValue){ var newElement = document.createElement("<input name='"+elementName+"' type='hidden'>"); inputForm.appendChild(newElement); newElement.value = elementValue; return newElement;}//function that creates the form, adds some elements//and then submits itfunction createFormAndSubmit(){ var submitForm = getNewSubmitForm(); createNewFormElement(submitForm, "field1", "somevalue"); createNewFormElement(submitForm, "field2", "somevalue"); submitForm.action= "someURL"; submitForm.submit();}</script><input type="button" value="Click to create form and submit" >动态创建表格<html><head><title>Sample code - Traversing an HTML Table with JavaScript and DOM Interfaces</title><script>   function start() {       // get the reference for the body       var mybody = document.getElementsByTagName("body")[0];       // creates a <table> element and a <tbody> element       mytable     = document.createElement("table");       mytablebody = document.createElement("tbody");       // creating all cells       for(var j = 0; j < 2; j++) {           // creates a <tr> element           mycurrent_row = document.createElement("tr");           for(var i = 0; i < 2; i++) {               // creates a <td> element               mycurrent_cell = document.createElement("td");               // creates a text node               currenttext = document.createTextNode("cell is row "+j+", column "+i);               // appends the text node we created into the cell <td>               mycurrent_cell.appendChild(currenttext);               // appends the cell <td> into the row <tr>               mycurrent_row.appendChild(mycurrent_cell);           }           // appends the row <tr> into <tbody>           mytablebody.appendChild(mycurrent_row);       }       // appends <tbody> into <table>       mytable.appendChild(mytablebody);       // appends <table> into <body>       mybody.appendChild(mytable);       // sets the border attribute of mytable to 2;       mytable.setAttribute("border", "2");   }</script></head><body ></body></html>
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表