จะเกิดข้อผิดพลาดขึ้นระหว่างพยายามที่จะเอาหัวเรื่องจากโครงสร้าง

0

คำถาม

ฉันต้องการความช่วยเหลือฉันกำลังจะองนี้เกิดข้อผิดพลาดขึ้นระหว่างพยายามที่จะเอาหัวเรื่องจากโครงสร้างของฉันไม่รู้วิธีซ่อมมันได้

ดรหัสนี้ทำงานแค่สบายดี


    function addCard(x,who) {
    
      if(who === 'player'){
        const  element = document.querySelector(".playercards");
        element.insertAdjacentHTML('afterbegin', `<button id='${x}' class='cardbutton'><p>${deck[x].title}</br></br>Action: ${deck[x].points} ${deck[x].action}</p></button>`);
      }
      if(who === 'bot'){
        const  element = document.querySelector(".botcards");
        element.insertAdjacentHTML('afterbegin', `<button id='bot${x}' class='cardbutton'><p>${deck[x].title}</br></br>Action: ${deck[x].points} ${deck[x].action}</p></button>`);
      }
    }
    
    playerCards.forEach((node, position) => addCard(node.getValue(),'player'));
    botCards.forEach((node, position) => addCard(node.getValue(),'bot'));

แต่ข้อผิดพลาดนี้จะเกิดขึ้นเมื่อฉันใช้รหัส deck[cardId].title มันไม่ได้ผลฉันไม่รู้ว่าทำไมฉันต้องพยายามจะทำให้แค่เบอร์มือนนี้ deck[1].title แต่มันก็ยังทำให้ฉันมีความเป็นข้อผิดพลาด ปัญหาอะไรมันอาจจะเป็น?

Uncaught TypeError: Cannot read properties of undefined (reading 'title')
    at HTMLUListElement.clickedOnCard (gamescript.js:47)

เกิดข้อผิดพลาดของภาพ

47 รอ


    console.log(deck[cardId].title())


    const deck = [
      {id: 0, title: 'Šokoladas', action: 'heal', points: 5},
      {id: 1, title: 'Sausainis', action: 'heal', points: 2},
      {id: 2, title: 'Pulti', action: 'damage', points: 3},
      {id: 3, title: 'Saukstas', action: 'damage', points: 1}
    ]

    document.querySelector(".playercards").addEventListener("click",clickedOnCard, false);
    
    function clickedOnCard(e) {
      if(e.target !== e.currentTarget){
        var cardId = e.target.id || e.srcElement.id;
        
        console.log(deck[cardId].title)
    
    
        // Deletes clicked player card
        removeCard(cardId);
    
        // Removes bot card
        removeCard(`bot${botCards.tail().getValue()}`);
        botCards.removeLast();
    
        updateHp();
    
        e.stopPropagation();
    
        postlog(getDate() + " Game: Button clicked." + " Used: " + cardId);
      }
    };

data-structures html javascript
2021-11-22 16:26:46
1

คำตอบที่ดีที่สุด

0

คุณต้อง iterate ของคุณอาเรย์เพื่อให้คุณรายการโปรดใช้ค้นหา

2021-11-22 16:30:32

ฉันแค่พยายามจะให้หมายเลขจาก html ธาตุ(ปุ่ม) document.querySelector(".playercards").addEventListener("click",clickedOnCard, false);
Man Tas

codeSandboxwould เป็นประโยชน์มากซะด้วยมันดูเหมือนว่าไม่ชัดเจนมาก
Anshuk

นี่คือทั้งรหัสของฉันฉันไม่รู้หรอกว่าใช้ยังไง codeSandbox ขอโทษ codesandbox.io/s/youthful-darkness-dcvok?file=/gamescript.js
Man Tas

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่