亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如果根據參數給table中的tr綁定不同事件

如果根據參數給table中的tr綁定不同事件

DIEA 2018-11-24 15:14:23
function update() {    var container = document.getElementById("ItemContainer");    container.innerHTML = "";    for(var i=0;i<this.bookMarkList.length;i++){                var name = this.ItemContainer[i].name;        var tr = document.createElement('tr');                      var td = document.createElement('td');        tr.appendChild(td);        tr.onclick =  function(){add(name);};       container.appendChild(tr);    }    }綁定相同函數,但是每個tr傳的參數不同,應該怎樣寫呢?,F在這樣寫每個tr都被綁定最新賦值的參數。
查看完整描述

1 回答

?
慕哥6287543

TA貢獻1831條經驗 獲得超10個贊

// 給你寫個demo吧

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<meta http-equiv="X-UA-Compatible" content="ie=edge">

<title>Document</title>

</head>

<style>


</style>

<body>

<table id="ItemContainer" border="1" width="100"></table>

<script>

    function add (name) {

        alert(name);

    }

    function update() {

        var container = document.getElementById("ItemContainer");

        console.log(container);

        container.innerHTML = "";

        for(let i=0; i<5; i++) {        

            let name = i;

            let tr = document.createElement('tr');              

            let td = document.createElement('td');

            td.innerHTML = i;

            tr.appendChild(td);

            console.log(name)

            tr.onclick = function (){

                return add(name);

            }; 

          container.appendChild(tr);

        }  

    }

    update()  

</script>

</body>

</html>


查看完整回答
反對 回復 2018-12-14
  • 1 回答
  • 0 關注
  • 579 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號