欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS/jQuery實(shí)現(xiàn)超簡單的Table表格添加,刪除行功能示例

 更新時(shí)間:2019年07月31日 10:15:25   作者:Love丶蘭  
這篇文章主要介紹了JS/jQuery實(shí)現(xiàn)超簡單的Table表格添加,刪除行功能,結(jié)合實(shí)例形式詳細(xì)分析了JS與jQuery針對(duì)Table表格添加,刪除行功能的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下

本文實(shí)例講述了JS/jQuery實(shí)現(xiàn)超簡單的Table表格添加,刪除行功能。分享給大家供大家參考,具體如下:

最近需要表格添加刪除行,添加行簡單,刪除行就有點(diǎn)兒懵逼了,于是網(wǎng)上找實(shí)例,功夫不負(fù)有心人,終于讓我找到了,下面的是代碼,大家覺得可以,記得點(diǎn)贊哦!!!謝謝!

需要的拿去,轉(zhuǎn)載請(qǐng)?jiān)诿黠@的位置標(biāo)注出出處!!!

<html>
<head>
    <title>table添加/刪除行</title>
</head>
<body>
<table id="testTbl" border=1>
    <tr>
        <td>性別</td>
        <td>姓名</td>
        <td>年齡</td>
    </tr>
    <tr>
        <td>
            <select name="a">
              <option value="男">男</option>
              <option value="女">女</option>
            </select>
        </td>
        <td>
            <input type="text" name="b">
        </td>
        <td>
            <input type="text" name="c">
        </td>
    </tr>
</table>
<input type="button" name="Submit2" value="添加" onclick="addRow()">
<script>
function addRow(){
    //添加行
    var newTr = testTbl.insertRow();
    //添加列
    var newTd0 = newTr.insertCell();
    var newTd1 = newTr.insertCell();
    var newTd2 = newTr.insertCell();
    var newTd3 = newTr.insertCell();
    //設(shè)置列內(nèi)容和屬性
    newTd0.innerText = document.all("a").options[document.all("a").selectedIndex].text;
    newTd1.innerText = document.all("b").value;
    newTd2.innerText = document.all("c").value;
    newTd3.innerHTML= '<input type="button" name="del" value="刪除" οnclick="del(this)">';
}
function del(o){
    //獲取表格
    var t=document.getElementById('testTbl');
    //刪除當(dāng)前行
    t.deleteRow(o.parentNode.parentNode.rowIndex)
}
</script>
</body>
</html>

//動(dòng)態(tài)添加行
function addRow(){
  var table = document.getElementById("tableID");
  var newRow = table.insertRow(); //創(chuàng)建新行
  var newCell1 = newRow.insertCell(); //創(chuàng)建新單元格
  newCell.innerHTML = ""; //單元格內(nèi)的內(nèi)容
  newCell.setAttribute("align","center"); //設(shè)置位置
}
//動(dòng)態(tài)刪除行
function deleteRow(){
  var rowIndex = event.srcElement.parentElement.parentElement.rowIndex;
  var styles = document.getElementById("tableID");
  styles.deleteRow(rowIndex);
}

用克隆的方式

<html>
<head>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript">
var index = 0;
$(document).ready(function(){
 $("button").click(function(){
 index++;
  $("body").append($("p").clone().attr({'id':'name'+index,'name':'pwd'+index}));
 });
});
</script>
</head>
<body>
<p>This is a paragraph.</p>
<button>復(fù)制每個(gè) p 元素,然后追加到 body 元素</button>
</body>
</html>

下面是直接添加和刪除當(dāng)前table表格,很好用

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <base href="<%=basePath%>" rel="external nofollow" >
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
  <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
 </head>
 <body>
  <a href = "javascript:void(0);" οnclick="a();">添加</a>
  <a href = "javascript:void(0);" οnclick="b();">顯示</a>
  <table id = "tab"></table>
 </body>
<script type="text/javascript">
    var index = 0;
    var a = function(){
        index++;
        var tab = "<table id = 'tab_"+index+"' border = '1' >";
        tab += "<tr><td colspan = '6' align = 'center'>尺寸規(guī)格</td></tr>";
        tab += "<tr><td>長度</td><td><input οnblur='b("+index+");' id = 'cd_"+index+"' /></td><td align = 'right'>寬度</td><td><input οnblur='b("+index+");' id = 'kd_"+index+"' /></td><td>數(shù)量(個(gè))</td><td><input οnblur='c("+index+");' id = 'sl_"+index+"' /></td></tr>";
        tab += "<tr><td>擺放區(qū)域</td><td><input id = 'bfqy_"+index+"' /></td><td>單個(gè)面積(平方米)</td><td><input readonly = 'readonly' id = 'dgmj_"+index+"' /></td><td>總面積</td><td><input readonly = 'readonly' id = 'zmj_"+index+"' /></td></tr>";
        tab += "<tr><td>內(nèi)容描述</td><td colspan = '4'><textarea rows='3' cols='70' id = 'content_"+index+"' ></textarea></td><td><input type = 'button' value = '刪除' onclick = 'del("+index+");' id = 'del_"+index+"' /></td></tr>";
        tab += "</table>";
        $('#tab').append(tab);
    }
    function del(ind){
        $('#tab_'+ind).remove();
    }
    function b(ind){
        var cdd = $('#cd_'+ind).val();
        var kdd = $('#kd_'+ind).val();
        if(cdd==''){cdd = 1;}
        if(kdd==''){kdd = 1;}
        if(cdd==''&&kdd==''){
            $('#dgmj_'+ind).val('');
        }else{
            $('#dgmj_'+ind).val(cdd * kdd);
        }
    }
    function c(ind){
        var cdd = $('#cd_'+ind).val();
        var kdd = $('#kd_'+ind).val();
        var sll = $('#sl_'+ind).val();
        if(cdd==''){cdd = 1;}
        if(kdd==''){kdd = 1;}
        if(sll==''){sll = 1;}
        if(cdd==''&&kdd==''&&sll==''){
            $('#zmj_'+ind).val('');
        }else if(cdd!=''&&kdd!=''&&sll!=''){
            $('#zmj_'+ind).val(cdd * kdd * sll);
        }else{
            $('#zmj_'+ind).val('');
        }
    }
</script>
</html>

感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運(yùn)行工具http://tools.jb51.net/code/HtmlJsRun測試上述代碼運(yùn)行效果。

更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript表格(table)操作技巧大全》、《JavaScript操作DOM技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》、《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》及《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)

希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • js實(shí)現(xiàn)簡單圖片切換

    js實(shí)現(xiàn)簡單圖片切換

    這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)簡單圖片切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • 最新評(píng)論