오류가 발생을 얻기 위해 노력하는 동안 제목에서 구조

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

당신은 반복 작업이 필요 당신의 배열을 얻을 당신의 항목,사용하시기 바랍 찾기

2021-11-22 16:30:32

내가 하려고 하 id 에서 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ý
..................................................................................................................