त्रुटि हो रही है, जबकि पाने के लिए कोशिश कर शीर्षक से संरचना

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

मैं सिर्फ पाने के लिए कोशिश कर आईडी से 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ý
..................................................................................................................

इस श्रेणी में लोकप्रिय

लोकप्रिय सवाल इस श्रेणी में