第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

在 forEach 數(shù)組中使用索引

在 forEach 數(shù)組中使用索引

夢(mèng)里花落0921 2022-08-27 09:42:53
我有一個(gè)對(duì)象數(shù)組顯示在表格中...我的目標(biāo)是通過單擊表中的某個(gè)特定項(xiàng)來訪問該項(xiàng)。然后,我將能夠添加/刪除類并訪問值,這最終是我需要做的。這就是我陷入困境的地方...myArray.forEach((item, index) => {// Sort through array, render to DOM  document.getElementById('myElementID').innerHTML +=    '<tr>' +    '<td>' +    item.thing +    '</td>' +    '<td' +    item.thing2 +    '</td>' +    '</tr>';// Completely stuck... I've added an event listener to each table row.  addEventListener('dblclick', () => {    console.log(//I want to log the index of the item I just clicked on);  });});請(qǐng)?jiān)徫?,如果這很容易,或者我做錯(cuò)了,但我對(duì)所有這些都很陌生,我無法以谷歌有幫助的方式組織我的問題。提前致謝。編輯 - 一些html根據(jù)要求...      <table id="myElementID">        <tr>          <th id="heading">Heading1</th>          <th id="anotherHeading">Heading2</th>        </tr>      </table>再次編輯(抱歉)...和一個(gè)JS小提琴。您將看到它記錄了兩個(gè)索引,而不僅僅是我單擊的索引。https://jsfiddle.net/c4pd5wmg/4/
查看完整描述

1 回答

?
慕尼黑8549860

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超11個(gè)贊

而不是搞砸索引等。您可以將事件處理程序附加到事件處理程序中,并且只在事件處理程序中引用。我還清理了您添加的tr。tre.target


const myArray= [{number: 45,otherNumber: 55},{number: 48,otherNumber:58}]


      myArray.forEach((item, index) => {

    

          let row = document.createElement("tr");

          let cell = document.createElement("td");

          cell.innerHTML = item.number;

          row.appendChild(cell);

          

          cell = document.createElement("td");

          cell.innerHTML = item.otherNumber;

          row.appendChild(cell);

    

          document.getElementById('myElementID').appendChild(row);

        

          row.addEventListener('dblclick', (e) => {

             console.log(e.target);

          });

        });

<table id="myElementID">

        <tr>

          <th id="heading">Heading1</th>

          <th id="anotherHeading">Heading2</th>

        </tr>

      </table>


查看完整回答
反對(duì) 回復(fù) 2022-08-27
  • 1 回答
  • 0 關(guān)注
  • 171 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)