为什么console.log记录$(this).data

Why is console.log logging $(this).data?

本文关键字:this data console log 记录 为什么      更新时间:2023-09-26

为什么我的控制台记录$(this).data?当我控制台记录变量时,我得到了未定义。

我需要访问chartType才能与代码一起移动:

if ($(this).hasClass("points")){
     chartType = $(this).data('chart-type');
     console.log($(this));
     console.log(chartType);
}

以下是它记录的内容(Stack Overflow不会让我把它的控制台放在第一行,但它确实存在)

undefined

您可以使用jquery attr()来选择属性。

if ($(this).hasClass("points")){
         chartType = $(this).attr('data-custom-value');
         console.log($(this));
         console.log(chartType);
    }