- document.createElement(). , , DOM. appendChild, DOM .
DOM ( ) :
<html>
<head>
<script>
function go (){
var newDiv = document.createElement ( "div" ); //
newDiv.innerHTML = ", !"; //
newDiv.style.backgroundColor = "red";
document.body.appendChild ( newDiv ) //
}
</script>
</head>
<body>
<inputtype= "button" onclick= "go()" value= "Go" />
</body>
</html>
, . insertBefore . , appendChild, , .
<html>
<head>
<script>
function go (){
var newDiv = document.createElement ( "div" ); //
var oldDiv = document.getElementById ( "id1" ); //
newDiv.innerHTML = ", !"; //
newDiv.style.backgroundColor = "blue";
document.body.insertBefore ( newDiv, oldDiv ) //
}
</script>
</head>
<body>
<divid= "id1" > </div>
<inputtype= "button" onclick= "go()" value= "Go" />
</body>
</html>
- removeChild . , parentNode. .
<html>
<head>
<script>
function add (){
var newDiv = document.createElement ( "div" ); //
newDiv.innerHTML = ", !"; //
newDiv.style.backgroundColor = "green";
newDiv.id = "id1";
document.body.appendChild ( newDiv ) //
}
function del1 (){
var toDel = document.getElementById ( "id1" ); //
document.body.removeChild ( toDel ) // body
}
function del2 (){
var toDel = document.getElementById ( "id1" ); //
toDel.parentNode.removeChild ( toDel ) //
}
</script>
</head>
<body>
<inputtype= "button" onclick= "add()" value= " " />
|
|
<inputtype= "button" onclick= "del1()" value= " 1" />
<inputtype= "button" onclick= "del2()" value= " 2" />
</body>
</html>
:
<html>
<head>
<script>
function add ( form ){
var newDiv = document.createElement ( "li" ); //<li>
newDiv.innerHTML = form.about.value + "<input type=\"button\" onclick=\"del(this)\" value=\"\"/>";
newDiv.style.backgroundColor = "#3FD3A7"; //
var list = document.getElementById ( "list1" ); // - id
list.appendChild ( newDiv ) //
}
function del ( toDel ){ //toDel - <input>
var toDel_parent = toDel.parentNode; // - <li>, <input>
toDel_parent.parentNode.removeChild ( toDel_parent ) //<li>
}
</script>
</head>
<body>
:
<ulid= "list1" >
</ul>
<form>
<inputtype= "text" name= "about" >
<inputtype= "button" onclick= "add(this.form)" value= " " />
</form>
</body>
</html>
DOM
(, ) DOM . , ( HTMLTableElement) .
table. rows [] ;
tr. cells [] ;
tr. rowIndex ;
td. cellIndex .
:
table. rows. length ;
tr. cells. length .
:
table. insertRow ( _ ) ;
table. deleteRow ( _ ) ;
tr. insertCell ( _ ) .
tr. deleteCell ( _ ) .
:
<html>
<head>
<script>
function add ( form ){
table1 = document.getElementById ( 'mytable' ); //
row1 = table1.insertRow ( table1.rows.length ); //
cell1 = row1.insertCell ( row1.cells.length ); // 1-
cell1.innerHTML = row1.rowIndex; //
cell1 = row1.insertCell ( row1.cells.length ); // 2-
if ( form.info.value != "" ) cell1.innerHTML = form.info.value //
else cell1.innerHTML = "default";
cell1 = row1.insertCell ( row1.cells.length ); // 3-
cell1.innerHTML = "<input type=\"checkbox\" name=\"check\">"; //
|
|
}
function del ( form ){
table1 = document.getElementById ( 'mytable' ); //
var i = table1.rows.length; // -
while ( i --){ //
var row1 = table1.rows [ i ] //
if ( row1.cells [ 2 ]. childNodes [ 0 ]. checked ){ //
table1.deleteRow ( row1.rowIndex ); //
}
}
}
</script>
</head>
<body>
:
<tableid= "mytable" border= "1" >
<tr>
<th> </th>
<th> </th>
<th> </th>
</tr>
<table>
<br>
<form>
<inputtype= "text" name= "info" >
<inputtype= "button" onclick= "add(this.form)" value= " " /><br>
<inputtype= "button" onclick= "del(this.form)" value= " " />
</form>
</body>
</html>
http://javascript.ru/forum/events/4237-dinamicheskoe-udalenie-strok-tablicy.html
http://javascript.ru/forum/events/2799-problemy-s-insertrow-i-insertcell.html
DOM
, JavaScript , , parent window. frames 0:
window.parent.frames[0].document.getElementById(My_elem)