<html>
<head>
<title>JS添加刪除元素</title>
<script type="text/javascript">
function $(nodeId)
{
return document.getElementByIdx(nodeId);
}
function $Name(tagName)
{
return document.getElementsByTagName_r(tagName);
}
function replaceMsg()
{
var newNode = document.createElement("P");//建立一個P標籤
newNode.innerHTML = "<font color='red'>替換後的文字</font>";
var oldNode = $Name("P")[0];//獲取body裏面第一個p元素
oldNode.parentNode.replaceChild(newNode,oldNode);//直接替換了標籤
}
function removeMsg()
{
var node = $("p2");//p標籤
var nodeBtn = $("remove");//按鈕
//node.parentNode.removeChild(node); //下面效果相同
document.body.removeChild(node);//在body中刪除id爲P2的元素
//nodeBtn.parentNode.removeChild(nodeBtn);//刪除該按鈕
document.body.removeChild(nodeBtn);
//document.body.removeNode();執行這條語句將清空body裏面的任何元素
}
function addbefore()
{
var newNode = document.createElement("p");//建立P標籤
//var newText = document.createTextNode("前面添加的元素");
//newNode.appendChild(newText);//與下面效果同樣
newNode.innerHTML = "<font color='red'>前面添加的元素</font>";//書寫P標籤裏面的內容
var oldNode = $("p3");//目標標籤
//document.body.insertBefore(newNode,oldNode);
oldNode.parentNode.insertBefore(newNode,oldNode);//在目標標籤前面添加元素
}
function addlast()
{
var newNode = document.createElement("p");//建立P標籤
//var newText = document.createTextNode("後面添加的元素");
//newNode.appendChild(newText);//與下面效果同樣
newNode.innerHTML = "<font color='red'>後面添加的元素</font>";
var oldNode = $("p3");
oldNode.appendChild(newNode);
//document.body.appendChild(newNode);//若是使用該方法,則在body的最後添加元素
}
window.onload = function addArrayMsg()
{
var arrayMsg = ['one','two','three','four','five'];//建立數組
var fragment = document.createDocumentFragment();//建立文檔片斷
var newNode ;
for (var i=0 ;i<arrayMsg.length ;i++ )
{
newNode = document.createElement("P");//建立P標籤
var nodeText = document.createTextNode(arrayMsg[i]);//建立文本標籤,value爲數組裏面的值
newNode.appendChild(nodeText);//
fragment.appendChild(newNode);//把P標籤追加到fragment裏面
}
document.body.appendChild(fragment);//把fragment追加到body裏面
}javascript
function addRow()
{
var tab = $("myTable");
//tab.createCaption().innerHTML="<font color='red'>個人表格</font>";
var oldTr = $("handleTr");
var newTr = tab.insertRow();//建立一行
var newTd1 = newTr.insertCell();//建立一個單元格
var newTd2 = newTr.insertCell();//建立一個單元格
newTd1.innerHTML = "<input type='checkbox' />";
newTd2.innerHTML = "<input type='text' />";
}
function removeRow()
{
var tab = $("myTable");
// if(tab.rows.length>0){
// tab.deleteRow();
// if(tab.rows.length==1)
// tab.deleteCaption();
// }
var cbbox ;
for(var i=0;i<tab.rows.length;i++){
cbbox = tab.rows[i].childNodes[0].childNodes[0];//獲取input元素
if(cbbox.checked){
tab.deleteRow(i--);
}
}
}
//全選
function selAll(value){
var items = document.all.tags("input");//獲取頁面上全部的input元素
for(var i = 0;i<items.length;i++){
if(items[i].type=="checkbox"){//判斷類型是否爲checkbox
items[i].checked = value.checked;
}
}
}
function getInputValue()
{
var inputArray = new Array();//建立一個數組
var tab = $("myTable");
var tbInput;
for(var i=0;i<tab.rows.length;i++){
tbInput = tab.rows[i].childNodes[1].childNodes[0].value;
if(tbInput!=""&&tbInput!=null)
inputArray.push(tbInput);
}
inputArray = inputArray.join("*^&");//默認以","號鏈接
$("showValue").value = inputArray;
}
var x ='10+20';
("alert(x);")
</script>
</head>
<body>
<p id="p1">Hello World!</p><input type="button" value="替換內容" onclick="replaceMsg();" />
<p id="p2">我能夠被刪除!</p><input type="button" id="remove" value="刪除它" onclick="removeMsg();" />
<p id="p3">在我上下添加一個元素吧!</p><input type="button" id="addbefore" value="前面添加" onclick="addbefore();" />
<input type="button" id="addlast" value="後面添加" onclick="addlast();" />
<p></p>
<div style="border:1px solid blue;height:300px">
<table id="myTable" cellpadding="0" cellspacing="0" border="1px solid blue" style="padding:4px;">
</table>html