javascript - 怎么在json里面引用本身的属性?
怪我咯
怪我咯 2017-04-11 11:17:40
[JavaScript讨论组]
 $('.p1').highcharts({
     xAxis: {
        title: {
            text: 'x轴标题'
        },
        labels: {
            name:['小明','小红','小名','小黑'],
            index:0,
            formatter: function() {
               console.log(this.name); //想要获取name属性却获取不到,有什么办法解决吗
            }
        }
    },
]})

想要获取name属性却获取不到,有什么办法解决吗

怪我咯
怪我咯

走同样的路,发现不同的人生

全部回复(4)
伊谢尔伦
var names = ['小明','小红','小名','小黑'];
$('.p1').highcharts({
     xAxis: {
        title: {
            text: 'x轴标题'
        },
        labels: {
            name: names,
            index:0,
            formatter: function() {
               console.log(names); //想要获取name属性却获取不到,有什么办法解决吗
            }
        }
    },
]})

这里只能这样做了,因为js里的this是动态的,在调用的时候才知道this是谁

巴扎黑

把参数定义成外部变量,使用this

PHP中文网

这里可以用this.point.name获取name属性

引用一段API

formatter: Function
Callback JavaScript function to format the data label. Note that if a format is defined, the format takes precedence and the formatter is ignored. Available data are:
this.percentage    Stacked series and pies only. The point's percentage of the total.
this.point    The point object. The point name, if defined, is available through this.point.name.
this.series:    The series object. The series name is available through this.series.name.
this.total    Stacked series only. The total value at this point's x value.
this.x:    The x value.
this.y:    The y value.
PHP中文网

你可以console.log用控制台查看一下this到底是什么,然后取出相应的值吧

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号