DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> 兼容FF和IE的動態table示例自寫
兼容FF和IE的動態table示例自寫
編輯:關於JavaScript     
HTML的table結構如下:
復制代碼 代碼如下:
<table id="Dy_table" width="760" cellpadding="0" style="border-top: solid 1px #9cf"
class="tableStyle1" cellspacing="0">
<tr>
<th style="width: 40px">序號<input id="pageRows" name="pageRows" type="hidden" value="1" />
<input type="hidden" name="HF_tableRows" id="HF_tableRows" value="1" /></th>
<th style="width:120px">零件型號</th>
<th style="width:130px">零件名稱</th>
<th style="width:45px">數量</th>
<th style="width:70px">無稅價</th>
<th style="width:70px">含稅價</th>
<th style="width:70px">稅額</th>
<th style="width:70px">貨款</th>
<th style="width:70px">整額</th>
<th style="width:60px">操作</th>
</tr>
<tr>
<td>1</td>
<td><input name='Dy_text_ljh' style='width: 110px' ondblclick='selectLj(this)' type='text'
readonly='true' /><input name='Dy_hd_cpdm' type='hidden' onpropertychange='textChange(this)'
title='產品代碼' /><input name='Dy_hd_mxid' type='hidden' value='' title='該行的Id,用來修改和刪除' /><input
name='Dy_hd_rowState' type='hidden' value='1' title='該行的狀態' /><input name='Dy_hf_ljgg'
type='hidden' value='0' title='零件規格' /></td>
<td><input name='Dy_text_cpmc' style='width: 120px' readonly='true' type='text' /></td>
<td><input name='Dy_text_sl' value='1' onkeypress='onlyNumberIn1(this)' onkeyup='textChange(this)'
style='width: 35px' type='text' /></td>
<td><input name='Dy_text_wsj' style='width: 60px' type='text' readonly='true' /></td>
<td><input name='Dy_text_hsj' style='width: 60px' type='text' readonly='true' /></td>
<td><input name='Dy_text_se' style='width: 60px' type='text' readonly='true' /></td>
<td><input name='Dy_text_hk' style='width: 60px' type='text' readonly='true' /></td>
<td><input name='Dy_text_ze' style='width: 60px' type='text' readonly='true' /></td>
<td><input name='del' type='button' value='刪 除' class='input-button' onclick='delnode1(this)' /></td>
</tr>
</table>

js代碼如下:
復制代碼 代碼如下:
function addEvent (o,c,h){
if(o.attachEvent){
o.attachEvent('on'+c,h);
}else{
o.addEventListener(c,h,false);
}
return true;
}
var selectRow;//頁面級js變量,用來存被選中的行,好在彈出窗口中對該行賦值
function addnode(){
var table=document.getElementById("Dy_table");
var tr=table.rows[1].cloneNode(true);
for(var i=1;i<tr.childNodes.length-1;i++){
for(var p=0;p<tr.childNodes[i].getElementsByTagName("input").length;p++){
if(tr.childNodes[i].getElementsByTagName("input")[p].name=="Dy_hd_rowState")//行狀態特殊對待
tr.childNodes[i].getElementsByTagName("input")[p].value="1";
else
tr.childNodes[i].getElementsByTagName("input")[p].value="";
}
}
var rowCount = table.rows[0].cells[0].getElementsByTagName("input")[1].value;//用戶可見的行數
tr.firstChild.innerHTML=parseInt(rowCount)+1;
table.rows[0].cells[0].getElementsByTagName("input")[1].value = parseInt(rowCount)+1;//可見行數+1
table.rows[0].cells[0].getElementsByTagName("input")[0].value = table.rows.length;//總行數,包含隱藏的
var tbody=table.getElementsByTagName("tbody");
if(tbody!=null){
tbody[0].appendChild(tr);
}else
table.appendChild(tr);
}
//刪除時的事件
function delnode(){
var table=document.getElementById("Dy_table");
var rowCount = table.rows[0].cells[0].getElementsByTagName("input")[1].value;//用戶可見的行數
var row;//獲取最後一個可見的row
for( var i=table.rows.length-1; i>=0 ;i--){
if(table.rows[i].style.display!="none")
{
row=table.rows[i];
break;
}
}
var rowId=row.cells[1].getElementsByTagName("input")[2].value;
if( rowCount > 1 ){
if(rowId=="")//新增的行未寫入數據庫時,直接刪除
{
var tbody=table.getElementsByTagName("tbody");
if(tbody!=null){
tbody[0].removeChild(row);
}else
table.removeChild(row);
table.rows[0].cells[0].getElementsByTagName("input")[1].value = parseInt(rowCount) - 1;
}
else//需要從數據庫刪除的,置上刪除標記
{
row.style.display="none";
row.cells[1].getElementsByTagName("input")[3].value = "2";
table.rows[0].cells[0].getElementsByTagName("input")[1].value = parseInt(rowCount)-1;
}
}else{
if(rowId == ""){//新增的行未寫入數據庫時,清空
row.cells[1].getElementsByTagName("input")[0].value="";
row.cells[1].getElementsByTagName("input")[1].value="";
row.cells[1].getElementsByTagName("input")[2].value="";
row.cells[1].getElementsByTagName("input")[3].value="";
row.cells[1].getElementsByTagName("input")[4].value="";
row.cells[2].getElementsByTagName("input")[0].value="";
row.cells[3].getElementsByTagName("input")[0].value="1";
row.cells[4].getElementsByTagName("input")[0].value="";
row.cells[5].getElementsByTagName("input")[0].value="";
row.cells[6].getElementsByTagName("input")[0].value="";
row.cells[7].getElementsByTagName("input")[0].value="";
row.cells[8].getElementsByTagName("input")[0].value="";
}else{//需要從數據庫刪除的,置上刪除標記
row.style.display="none";
row.cells[1].getElementsByTagName("input")[3].value = "2";
table.rows[0].cells[0].getElementsByTagName("input")[1].value = parseInt(rowCount) - 1;
addnode();
}
}
setClf();
}
//刪除時的事件
function delnode1(o){
var tr=o.parentNode.parentNode;
var table=document.getElementById("Dy_table");
var rowCount = table.rows[0].cells[0].getElementsByTagName("input")[1].value;//用戶可見的行數
var rowId=tr.cells[1].getElementsByTagName("input")[2].value;
if( rowCount > 1 ){
if(rowId=="")//新增的行未寫入數據庫時,直接刪除
{
var tbody=table.getElementsByTagName("tbody");
if(tbody!=null){
tbody[0].removeChild(tr);
}else
table.removeChild(tr);
table.rows[0].cells[0].getElementsByTagName("input")[1].value = parseInt(rowCount) - 1;
}
else
{
tr.style.display="none";
tr.cells[1].getElementsByTagName("input")[3].value = "2";
table.rows[0].cells[0].getElementsByTagName("input")[1].value = parseInt(rowCount) - 1;
}
}else{
if(rowId==""){//新增的行未寫入數據庫時,直接清空
tr.cells[1].getElementsByTagName("input")[0].value="";
tr.cells[1].getElementsByTagName("input")[1].value="";
tr.cells[1].getElementsByTagName("input")[2].value="";
tr.cells[1].getElementsByTagName("input")[3].value="";
tr.cells[1].getElementsByTagName("input")[4].value="";
tr.cells[2].getElementsByTagName("input")[0].value="";
tr.cells[3].getElementsByTagName("input")[0].value="1";
tr.cells[4].getElementsByTagName("input")[0].value="";
tr.cells[5].getElementsByTagName("input")[0].value="";
tr.cells[6].getElementsByTagName("input")[0].value="";
tr.cells[7].getElementsByTagName("input")[0].value="";
tr.cells[8].getElementsByTagName("input")[0].value="";
}else{//需要從數據庫刪除的,置上刪除標記
tr.style.display="none";
tr.cells[1].getElementsByTagName("input")[3].value = "2";
table.rows[0].cells[0].getElementsByTagName("input")[1].value = parseInt(rowCount) - 1;
addnode();
}
}
//以下循環用於從中間刪除時更新表格行號
for( var i= 1,p = 1; i < table.rows.length ;i++){
if(table.rows[i].style.display!="none")
{
table.rows[i].cells[0].innerHTML = p;
p++;
}
}
setClf();
}
//修改時發生的事件,改變行狀態
function textChange(o){
var tr=o.parentElement.parentElement;
if(o.parentElement.parentElement.parentElement==null)return;//如果是新增加的行則返回
var rowState = tr.cells[1].getElementsByTagName("input")[3].value;
if( rowState == "1")
return;
else
tr.cells[1].getElementsByTagName("input")[3].value = "3";
setClf();
}
//提交前驗證數據,保證沒有重復的行
function checkSameData(){
var table=document.getElementById("Dy_table");
for( var i= 1; i < table.rows.length ;i++){
if(table.rows[i].style.display == "none"||table.rows[i].cells[1].getElementsByTagName("input")[1].value=="") continue;
for( var p= i + 1; p < table.rows.length ;p++){
if(table.rows[p].style.display == "none") continue;
if(table.rows[i].cells[1].getElementsByTagName("input")[1].value.replace(/\s+$/g,"") ==
table.rows[p].cells[1].getElementsByTagName("input")[1].value.replace(/\s+$/g,""))
{alert("零件部分存在重復的項,不能保存!");return false;}
}
}
return true;
}
var dialogWin;//零件窗口是否打開
//選零件
function selectLj(o){
if(dialogWin == null){
selectRow = o.parentNode.parentNode;//將行賦值給全局變量
var cpxh = selectRow.cells[1].getElementsByTagName("input")[0].value;
dialogWin = winOpen('selectLj.aspx?ljh='+cpxh);
// window.open("../jddgl/Select_lj.aspx?ljh=" + cpxh,window,
// "center:yes;dialogWidth:600px;dialogHeight:400px;help:no;status:no;");
}
}
function winOpen(url){
return window.open(url,'selectLj','resizable=1,status=0,menubar=0,scrollbars=1,height=400px,width=600px');
}

//計算table內費用
function setClf(){}

這算是對之前寫的動態增加表格的改進,之前那個實在是學習了js沒多久而作的失敗作品。現在這個總算是可以兼容FF和IE了。在兼容的過程中,沒少看標准dom規范,提高了不少知識,js操作dom翻看ms的DHTML手冊的時候要注意它裡面提到的方法和屬性是不是標准的,最好用標准的。

此動態table只要HTML裡定好了table就可以動態的增減,不用關心它有多少個td,注意在第二個td裡面加上相關的input hidden。動態增刪只是一個表面的功能,這個table和dataset一樣具有一個行狀態,用行狀態可以在服務器端對數據進行更新、刪除和新增。1新增,2刪除,3修改。只是用彈出窗口在FF和IE7下效果不行,不知道用iframe效果怎麼樣。

IE下對於clone出來的tr不能通過cells來獲取td的集合,FF下則是可以。由於tr內有input用了onpropertychange事件,在去掉新增的tr內input值的時候也會觸發,所以在這個事件裡用一個if排除了這種情況。浏覽器的兼容還真是有些麻煩。FF下面還存在一個問題,從沒有提交頁面的情況下,FF重新載入頁面的時候,服務器端控件的值會被保存下來,而IE下則是真的重新載入,頁面上的任何值都不會保留。FF的這個保存服務器控件值的行為應該是它對asp.net支持存在問題,沒有提交頁面的情況下這是不應該發生的。
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved