« PHP采集利器:Snoopy 试用心得 [转]firefox中的ajax 问题 »

appendchild介绍

1、先用document.createElement方法创建一个input元素!

程序代码
var newInput = document.createElement("input");

2、设定相关属性,如name,type等

程序代码
newInput.type=mytype;
newInput.name="input1";

3、用appendChild方法,将元素追加到某个标签内容中!

程序代码
TemO.appendChild(newInput);

Javascript部分代码:

程序代码
<script language="javascript">
function AddElement(mytype){
var mytype,TemO=document.getElementById("add");
var newInput = document.createElement("input");
newInput.type=mytype;
newInput.name="input1";
TemO.appendChild(newInput);
var newline= document.createElement("br");//创建一个BR标签是为能够换行!
TemO.appendChild(newline);
}
</script>

测试全部代码:
<html >
<head>
<title>动态添加表单元素-<a href=http://subin.org.cn target="_blank">subin</a>_<a href=http://subin.org.cn/blog target="_blank">subin blog</a></title>
</head>
<script language="javascript">
function AddElement(mytype){
var mytype,TemO=document.getElementById("add");
var newInput = document.createElement("input");
newInput.type=mytype;
newInput.name="input1";
TemO.appendChild(newInput);
var newline= document.createElement("br");
TemO.appendChild(newline);
}
</script>
<body>
 <form action="" method="get" name="frm">
 <div id="add">
     <input type="text" name="textfield"><br>
</div>
 </form>
 <input name="" type="button" value="新建文本框" onClick="AddElement('text')" />
 <input name="" type="button" value="新建复选框" onClick="AddElement('checkbox')" />
  <input name="" type="button" value="新建单选框" onClick="AddElement('radio')" />
  <input name="" type="button" value="新建文件域" onClick="AddElement('file')" />
  <input name="" type="button" value="新建密码框" onClick="AddElement('password')" />
  <input name="" type="button" value="新建提交按钮" onClick="AddElement('submit')" />
  <input name="" type="button" value="新建恢复按钮" onClick="AddElement('reset')" />
</body>
</html>


 

js appendchild()适用对象
 

<a>, <acronym>, <address>, <applet>, <area>, attribute, <b>, <base>, <basefont>,
<bdo>, <big>, <blockquote>, <body>, <br>, <button>, <caption>, <center>, <cite>,
<code>, <col>, <colgroup>, <comment>, <dd>, <del>, <dfn>, <dir>, <div>, <dl>, <dt>,
<em>, <fieldset>, <font>, <form>, <frame>, <frameset>, <head>, <hn>, <hr>, <html>,
<i>, <iframe>, <img>, <input type="button">, <input type="checkbox">, <input
type="file">, <input type="image">, <input type="password">, <input type="radio">,
<input type="reset">, <input type="submit">, <input type="text">, <ins>, <isindex>,
<kbd>, <label>, <legend>, <li>, <link>, <listing>, <map>, <marquee>, <menu>,
<meta>, <noframes>, <noscript>, <object>, <ol>, <optgroup>, <option>, <p>, <param>,
<plaintext>, <pre>, <q>, <s>, <samp>, <script>, <select>, <small>, <span>,
<strike>, <strong>, <style>, <sub>, <sup>, <table>, <tbody>, <td>, <textarea>,
<tfoot>, <th>, <thead>, <title>, <tr>, <tt>, <u>, <ul>, <var>, <xmp>

 

例子:

<html >
<head>
    <title>appendChild</title>
</head>

<script language="javascript" type="text/javascript">
function B1(){
   //appendChild 将新建的元素至于要附加的对象内,createElement 在文档内创建一个对象element,以待引用
   var myElement = document.createElement('<div style="width:600px; height:200px;background-color:blue;"></div>');
   document.all.TextArea1.appendChild(myElement);
}
</script>

<body id="myBody">
    <button onclick="B1();">
        Append child</button>
    <a href="#" id="A1"></a>
    <br />
    <textarea id="TextArea1" style="overflow-x: scroll; overflow-y: scroll; height: 100px;
        width: 300px;"></textarea>
</body>
</html>

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

网站目录

最近发表

最新评论及回复

文章归档

Search

Powered By Z-Blog 1.8 Terminator(beta) Build 71218 Designed by Michael

Copyright 0-9999 subin.org.cn Rights Reserved. 晋ICP备08000685号