最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

兼容FF和IE的動(dòng)態(tài)table示例自寫

 更新時(shí)間:2013年10月21日 16:56:42   作者:  
Js實(shí)現(xiàn)的動(dòng)態(tài)增加表格的文章有很多,但大多數(shù)都不兼容FF和IE,于是手寫了一個(gè),經(jīng)測(cè)試效果還不錯(cuò),特此與大家分享,感興趣的朋友不要錯(cuò)過(guò)
HTML的table結(jié)構(gòu)如下:
復(fù)制代碼 代碼如下:

<table id="Dy_table" width="760" cellpadding="0" style="border-top: solid 1px #9cf"
class="tableStyle1" cellspacing="0">
<tr>
<th style="width: 40px">序號(hào)<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">零件型號(hào)</th>
<th style="width:130px">零件名稱</th>
<th style="width:45px">數(shù)量</th>
<th style="width:70px">無(wú)稅價(jià)</th>
<th style="width:70px">含稅價(jià)</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='產(chǎn)品代碼' /><input name='Dy_hd_mxid' type='hidden' value='' title='該行的Id,用來(lái)修改和刪除' /><input
name='Dy_hd_rowState' type='hidden' value='1' title='該行的狀態(tài)' /><input name='Dy_hf_ljgg'
type='hidden' value='0' title='零件規(guī)格' /></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代碼如下:
復(fù)制代碼 代碼如下:

function addEvent (o,c,h){
if(o.attachEvent){
o.attachEvent('on'+c,h);
}else{
o.addEventListener(c,h,false);
}
return true;
}
var selectRow;//頁(yè)面級(jí)js變量,用來(lái)存被選中的行,好在彈出窗口中對(duì)該行賦值
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")//行狀態(tài)特殊對(duì)待
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;//用戶可見的行數(shù)
tr.firstChild.innerHTML=parseInt(rowCount)+1;
table.rows[0].cells[0].getElementsByTagName("input")[1].value = parseInt(rowCount)+1;//可見行數(shù)+1
table.rows[0].cells[0].getElementsByTagName("input")[0].value = table.rows.length;//總行數(shù),包含隱藏的
var tbody=table.getElementsByTagName("tbody");
if(tbody!=null){
tbody[0].appendChild(tr);
}else
table.appendChild(tr);
}
//刪除時(shí)的事件
function delnode(){
var table=document.getElementById("Dy_table");
var rowCount = table.rows[0].cells[0].getElementsByTagName("input")[1].value;//用戶可見的行數(shù)
var row;//獲取最后一個(gè)可見的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=="")//新增的行未寫入數(shù)據(jù)庫(kù)時(shí),直接刪除
{
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//需要從數(shù)據(jù)庫(kù)刪除的,置上刪除標(biāo)記
{
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 == ""){//新增的行未寫入數(shù)據(jù)庫(kù)時(shí),清空
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{//需要從數(shù)據(jù)庫(kù)刪除的,置上刪除標(biāo)記
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();
}
//刪除時(shí)的事件
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;//用戶可見的行數(shù)
var rowId=tr.cells[1].getElementsByTagName("input")[2].value;
if( rowCount > 1 ){
if(rowId=="")//新增的行未寫入數(shù)據(jù)庫(kù)時(shí),直接刪除
{
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==""){//新增的行未寫入數(shù)據(jù)庫(kù)時(shí),直接清空
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{//需要從數(shù)據(jù)庫(kù)刪除的,置上刪除標(biāo)記
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();
}
}
//以下循環(huán)用于從中間刪除時(shí)更新表格行號(hào)
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();
}
//修改時(shí)發(fā)生的事件,改變行狀態(tài)
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();
}
//提交前驗(yàn)證數(shù)據(jù),保證沒有重復(fù)的行
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("零件部分存在重復(fù)的項(xiàng),不能保存!");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');
}

//計(jì)算table內(nèi)費(fèi)用
function setClf(){}

這算是對(duì)之前寫的動(dòng)態(tài)增加表格的改進(jìn),之前那個(gè)實(shí)在是學(xué)習(xí)了js沒多久而作的失敗作品?,F(xiàn)在這個(gè)總算是可以兼容FF和IE了。在兼容的過(guò)程中,沒少看標(biāo)準(zhǔn)dom規(guī)范,提高了不少知識(shí),js操作dom翻看ms的DHTML手冊(cè)的時(shí)候要注意它里面提到的方法和屬性是不是標(biāo)準(zhǔn)的,最好用標(biāo)準(zhǔn)的。

此動(dòng)態(tài)table只要HTML里定好了table就可以動(dòng)態(tài)的增減,不用關(guān)心它有多少個(gè)td,注意在第二個(gè)td里面加上相關(guān)的input hidden。動(dòng)態(tài)增刪只是一個(gè)表面的功能,這個(gè)table和dataset一樣具有一個(gè)行狀態(tài),用行狀態(tài)可以在服務(wù)器端對(duì)數(shù)據(jù)進(jìn)行更新、刪除和新增。1新增,2刪除,3修改。只是用彈出窗口在FF和IE7下效果不行,不知道用iframe效果怎么樣。

IE下對(duì)于clone出來(lái)的tr不能通過(guò)cells來(lái)獲取td的集合,F(xiàn)F下則是可以。由于tr內(nèi)有input用了onpropertychange事件,在去掉新增的tr內(nèi)input值的時(shí)候也會(huì)觸發(fā),所以在這個(gè)事件里用一個(gè)if排除了這種情況。瀏覽器的兼容還真是有些麻煩。FF下面還存在一個(gè)問題,從沒有提交頁(yè)面的情況下,F(xiàn)F重新載入頁(yè)面的時(shí)候,服務(wù)器端控件的值會(huì)被保存下來(lái),而IE下則是真的重新載入,頁(yè)面上的任何值都不會(huì)保留。FF的這個(gè)保存服務(wù)器控件值的行為應(yīng)該是它對(duì)asp.net支持存在問題,沒有提交頁(yè)面的情況下這是不應(yīng)該發(fā)生的。

相關(guān)文章

  • js滑動(dòng)提示效果代碼分享

    js滑動(dòng)提示效果代碼分享

    這篇文章主要為大家分享了js滑動(dòng)提示效果代碼,實(shí)現(xiàn)方法簡(jiǎn)單,感興趣的小伙伴們可以參考一下
    2016-03-03
  • Bootstrap 3多級(jí)下拉菜單實(shí)例

    Bootstrap 3多級(jí)下拉菜單實(shí)例

    這篇文章主要為大家詳細(xì)介紹了Bootstrap 3多級(jí)下拉菜單實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • JavaScript onclick與addEventListener使用的區(qū)別介紹

    JavaScript onclick與addEventListener使用的區(qū)別介紹

    addEventListener()方法用于向指定元素添加事件句柄,使用 removeEventListener()方法來(lái)移除,onclick和addEventListener事件區(qū)別是:onclick事件會(huì)被覆蓋,而addEventListener可以先后運(yùn)行不會(huì)被覆蓋,addEventListener可以監(jiān)聽多個(gè)事件
    2022-09-09
  • TypeScript與JavaScript項(xiàng)目里引入MD5校驗(yàn)和

    TypeScript與JavaScript項(xiàng)目里引入MD5校驗(yàn)和

    這篇文章主要介紹了TypeScript與JavaScript項(xiàng)目里引入MD5校驗(yàn)和,MD5校驗(yàn)和可以用于驗(yàn)證網(wǎng)絡(luò)文件傳輸?shù)耐暾砸约胺乐刮募蝗舜鄹?。下文我們就一起?lái)學(xué)習(xí)TypeScript與JavaScript項(xiàng)目里引入MD5校驗(yàn)和_MD5校驗(yàn),需要的朋友可以參考一下
    2022-02-02
  • vue?element?loading遮罩層添加按鈕功能實(shí)現(xiàn)

    vue?element?loading遮罩層添加按鈕功能實(shí)現(xiàn)

    這篇文章主要介紹了vue?element?loading遮罩層添加按鈕功能實(shí)現(xiàn),通過(guò)實(shí)例代碼補(bǔ)充介紹了vue+elementui的this.$loading遮罩使用方法,需要的朋友可以參考下
    2024-03-03
  • Electron啟動(dòng)出現(xiàn)白屏問題的解決方案

    Electron啟動(dòng)出現(xiàn)白屏問題的解決方案

    對(duì)于 Web 開發(fā)者使用 Electron 構(gòu)建桌面應(yīng)用程序時(shí),經(jīng)常會(huì)遇到如上圖所示的一個(gè)問題,在應(yīng)用窗口創(chuàng)建完成到頁(yè)面加載出來(lái)的這段時(shí)間里,出現(xiàn)了長(zhǎng)時(shí)間的白屏,本文就來(lái)探索基于 Electron 場(chǎng)景下啟動(dòng)白屏的解決方案,需要的朋友可以參考下
    2024-05-05
  • 精通Javascript系列之?dāng)?shù)據(jù)類型 字符串

    精通Javascript系列之?dāng)?shù)據(jù)類型 字符串

    下面先講一下字符串String字符串由零個(gè)或者多個(gè)字符構(gòu)成。字符可以包括字母、數(shù)字、標(biāo)點(diǎn)符號(hào)和空格。
    2011-06-06
  • JS簡(jiǎn)單實(shí)現(xiàn)查看文檔創(chuàng)建日期、修改日期和文檔大小的方法示例

    JS簡(jiǎn)單實(shí)現(xiàn)查看文檔創(chuàng)建日期、修改日期和文檔大小的方法示例

    這篇文章主要介紹了JS簡(jiǎn)單實(shí)現(xiàn)查看文檔創(chuàng)建日期、修改日期和文檔大小的方法,結(jié)合實(shí)例形式分析了JavaScript使用fileCreatedDate屬性、fileModifiedDate屬性、lastModified屬性和fileSize屬性相關(guān)操作技巧,需要的朋友可以參考下
    2018-04-04
  • 微信小程序Page中data數(shù)據(jù)操作和函數(shù)調(diào)用方法

    微信小程序Page中data數(shù)據(jù)操作和函數(shù)調(diào)用方法

    這篇文章主要介紹了微信小程序Page中data數(shù)據(jù)操作和函數(shù)調(diào)用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • moment.js 時(shí)間日期處理詳解

    moment.js 時(shí)間日期處理詳解

    這篇文章主要介紹了moment.js 時(shí)間日期處理詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09

最新評(píng)論

泸定县| 大化| 苍南县| 登封市| 阳曲县| 大新县| 东方市| 孟村| 信丰县| 潼南县| 金秀| 正阳县| 襄城县| 乐陵市| 甘南县| 黎川县| 广饶县| 阿合奇县| 沙河市| 肃宁县| 西乡县| 郓城县| 贵溪市| 疏附县| 新兴县| 湘潭县| 珲春市| 日土县| 崇义县| 景德镇市| 石泉县| 高清| 沾化县| 溆浦县| 综艺| 济宁市| 道孚县| 陇川县| 会东县| 高雄市| 庆城县|