2015-07-02 63 views
-2

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

我需要访问chartType沿着与代码移动:

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

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

下面有什么,它记录 (堆栈溢出不会让我把什么安慰的第一线,但它的存在)

undefined 
+0

这是怎么回事? –

+0

可能不确定你的意思。但它在点击功能中发生。它将在控制台中输出未定义的内容。对不起,如果这不是你所需要的。 –

回答

0

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

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

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

这仍然不适用于我。这是我的按钮的HTML。 –

+0

这肯定会起作用 –

相关问题