【问题标题】:Finding parent of highcharts button w/ jquery使用 jquery 查找 highcharts 按钮的父级
【发布时间】:2013-10-08 13:09:47
【问题描述】:

我的 highcharts 图表中有一个使用以下代码的按钮:

exporting: {
    buttons: {
        backButton: {
            _titleKey: 'backTitle',
            enabled: theChart.buttonOn,
            x: 0,
            y: 300,
            onclick: function () {
                $(this).parents(".chart").data('chart', $(this).parents(".chart").data('mainChart'));
                $(this).parents(".chart").trigger('redoChart');
            },
            text: 'Click to return to full graph',
            width:200,
            theme: {
                'stroke-width': 1,
                stroke: 'black',
                fill: '#cccccc',
                states: {
                    hover: {
                        'stroke-width': 1,
                        stroke: 'black',
                        fill: '#cccccc'
                    },
                    select: {
                        'stroke-width': 1,
                        stroke: 'black',
                        fill: '#cccccc'
                    }
                }
            }
        }
    }
}

当我点击这个按钮时,什么也没有发生。我已经确定这是因为$(this).parents(".chart") 没有正确识别我的图表的容器 div(它确实具有“图表”类)。如果我将$(this).parents(".chart") 替换为$("#thechart"),一切都很好。

【问题讨论】:

  • 我们需要查看 HTML 以及 this 所指的内容。此外,如果您的图表有 ID,请使用它。
  • 我的假设是 this 指的是按钮,因为这是按钮的“点击”功能......这是不正确的吗?
  • 它可能会,如果您可以发布包含该按钮的 HTML 和您的图表,我们可以验证
  • jsfiddle.net/ztfRS/1 有一个 jsfiddle 说明了这个问题。如果我们可以让 div 隐藏在那里,我可以在我的应用程序中实现该解决方案。
  • 首先,在这个你有图表对象,其次你需要隐藏什么?整个图表?如果是,为什么你不能使用 $('#chartDiv').hide() ?

标签: javascript jquery highcharts


【解决方案1】:

用途:

$(this.container).parents('.chart').hide();

在按钮处理程序this 中是一个highcharts 对象。 this.container 是在您的 div 中创建的 div 的 highcharts。因此,要隐藏整个事情,您要查找向其添加图表类的父级。

【讨论】:

  • 太棒了!谢谢!您介意解释一下发生了什么或为什么会这样吗?
  • @JohnChrysostom - 添加到答案中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-08
相关资源
最近更新 更多