js 删除 按钮所在的行

<body>

    <table id="delte">
        <caption>简易购物车</caption>
        <tr>
            <th class="goods">商品</th>
            <th>单价</th>
            <th>数量</th>
            <th>小计</th>
            <th>操作</th>
        </tr>
        <tr class="shop1" id="shop2">
            <td class="goods">小米MIX</td>
            <td id="price">5000</td>
            <td class="number">
                <input type="button" value="-" id="jian1" onclick="minus('price','number','total')">
                <input type="text" value="1" id="number" class="num" onblur="change('price','number','total')">
                <input type="button" value="+" id="jia1" onclick="add('price','number','total')">
            </td>
            <td id="total">5000</td>
            <td><a href="#" onclick="dele(this)">删除</a></td>
        </tr>
        <tr class="shop1" id="shop3">
            <td class="goods">iphone 8</td>
            <td id="price2">6000</td>
            <td class="number">
                <input type="button" value="-" id="jian2" onclick="minus('price2','count','total2')">
                <input type="text" value="1" id="count" class="num" onblur="change('price2','count','total2')">
                <input type="button" value="+" id="jia2" onclick="add('price2','count','total2')">
            </td>
            <td id="total2">6000</td>
            <td><a href="#" οnclick="dele(this)">删除</a>
            </td>
        </tr>
    </table>
</body>

 

 

 

方法一:

1         function dele(rows) {     //传值不可以用特殊字,如把rows 改成this,是没有删除效果的
2             var row = rows.parentNode.parentNode; // 按钮所在行
3             var index = row.rowIndex; // 当前行的索引
4             var tb = row.parentNode; //当前表格
5             tb.deleteRow(index);
6         }

方法二:

 function dele(obj) {    //传值
            var Row = obj.parentNode;
            while (Row.tagName.toLowerCase() != "tr") {
                Row = Row.parentNode;
            }
            Row.parentNode.removeChild(Row); //删除行

        }

 

转载于:https://www.cnblogs.com/yuer20180726/p/11281949.html

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值