【问题标题】:getting dataset value from a nodeList javascript从 nodeList javascript 获取数据集值
【发布时间】:2020-09-09 05:38:50
【问题描述】:

我有一个 div 的 nodeList,每个 div 都有一个带有 src、id 和数据集的 img。 当使用 forEach 循环循环 nodeList 时,我存储了该 div 的第一个元素(因此是 img),因此我可以访问它的 id、src 和数据集。我成功获得了 id 和 src 但没有获得数据集;它总是返回一个空的 DOMStringMap {}。

我怎样才能得到这些信息?

每个 div 都有一个类似的 img ->


        function getCards(){
            let totalCards = document.querySelectorAll(".card")

            totalCards.forEach(el => {

                el.addEventListener("click", function(){
                    let thisCard = el.children[0]
                    let thisCardID = thisCard.id
                    let thisCardDataSet = thisCard.dataset
                    let thisCardSRC = thisCard.src

                    console.log(thisCard)
                    console.log(thisCardID)
                    console.log(thisCardDataSet)
                    console.log(thisCardSRC)

                })

            })
            //console.log(flippedCard)
        }
´´´

【问题讨论】:

标签: javascript nodelist


【解决方案1】:

如果数据集是 img 标记的属性,如下所示:

<img src=".." alt=".." dataset="..." />

那么你需要先抓取属性对象

let thisCardDataSet = thisCard.attributes.dataset

【讨论】:

    猜你喜欢
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 2013-01-07
    • 1970-01-01
    • 2018-03-20
    • 1970-01-01
    • 2017-04-24
    • 1970-01-01
    相关资源
    最近更新 更多