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

时间:2015-07-02 10:33:30

标签: javascript jquery

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

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

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

     console.log($(this));
     console.log(chartType);
}

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

undefined

1 个答案:

答案 0 :(得分:0)

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

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

         console.log($(this));
         console.log(chartType);
    }
相关问题