javascript—使用createElement動態建立HTML對象.表單傳值

來源:互聯網
上載者:User

轉自:http://blog.csdn.net/fxh_hua/article/details/4433682  方便自己找

 

1.建立連結

 

<BODY>
<script language="JavaScript">
var o = document.body;
//建立連結
function createA(url,text)
{
    var a = document.createElement("a");
    a.href = url;
    a.innerHTML = text;
    a.style.color = "red";
    o.appendChild(a);
}
createA("http://www.hao123.com/","hao");
</script>
</BODY>

 

 

                 2.建立DIV <body>
<script language="javascript">
var o = document.body;
//建立DIV
function createDIV(text)
{
    var div = document.createElement("div");
    div.innerHTML = text;
    div.style.background = "red";
    o.appendChild(div);
}
createDIV("div");
</script>
</body>                             3.建立表單項 <BODY>
<script language="javascript">
var o = document.body;
//建立表單項
function createInput(sType,sValue)
{
    var input = document.createElement("input");
    input.type = sType;
    input.value = sValue;
    o.appendChild(input);
}
createInput("button","aa");
</script>
</BODY>                  4.建立表格 <BODY>
<script language="javascript">
var o = document.body;
//建立表格
function createTable(w,h,r,c)
{
    var table = document.createElement("table");
    var tbody = document.createElement("tbody");
    table.width = w;
    table.height = h;
    table.border = 1;
    for(var i=1;i<=r;i++)
    {
        var tr = document.createElement("tr");
        for(var j=1;j<=c;j++)
        {
            var td = document.createElement("td");
            td.innerHTML = i + "" + j;
            //td.appendChild(document.createTextNode(i + "" + j));
            td.style.color = "#FF0000";
            tr.appendChild(td);
        }
        tbody.appendChild(tr);
    }
    table.appendChild(tbody);
    o.appendChild(table);
}
createTable(270,270,9,9);
</script>
</BODY>       4.建立按鈕,文字框 並表單傳值(HTMLPage1.htm)   <script type="text/javascript">
                var index = 2;        function addEle() {
          
            var txt = document.createElement("input");
            txt.setAttribute("type", "input");
            txt.setAttribute("id", "txt" + index);            var btn = document.createElement("input");
            btn.setAttribute("type", "`");
            btn.setAttribute("id", "btn" + index);
            btn.setAttribute("value", "取值");
            btn.setAttribute("onclick", "openWindow()");            var br = document.createElement("br");            document.getElementById("content").appendChild(txt);
            document.getElementById("content").appendChild(btn);
            document.getElementById("content").appendChild(br);            index++;        }        function openWindow() {
            var returnVal = window.showModalDialog("HTMLPage2.htm", "", "");
            var srcID = window.event.srcElement.id;//觸發事件的對象.
            var ind = srcID.substr(3,srcID.length);
            for (var i in returnVal) {
                document.getElementById("txt" + ind).value += returnVal[i]+"  ";
            }
        }

    </script>                       HTMLPage2.htm     <script type="text/javascript">
   
        function sub() {
            var arr = document.getElementsByTagName("input");  
            var val = new Array();
            for (var i in arr) {
                if (arr[i].type == "checkbox") {
                    if (arr[i].checked) {
                        val[val.length] = arr[i].value;
                    }
                }
            }
            window.returnValue = val;
            window.close();
        }
       
    </script>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.