【发布时间】:2017-04-26 11:36:15
【问题描述】:
我创建了一个字段来为 wordpress 后台的 highchart 选择标签,如下所示:
public static function sm_register_chart() {
$charts = array();
if( function_exists( 'wdt_get_all_charts_nonpaged')){
foreach( wdt_get_all_charts_nonpaged() as $table ){
$charts[$table['id']] = $table['title'];
}
}
$captions = array();
$fields = array(
array(
'label' => esc_html( 'Graphic' ),
'description' => esc_html( 'Choose the graphic' ),
'attr' => 'chart',
'type' => 'select',
'options' => $charts,
),
array(
'label' => esc_html( 'Footer label' ),
'description' => esc_html( 'Choose the footer label' ),
'attr' => 'footer_caption',
'type' => 'text',
),
);
我想将在footer_caption 中输入的任何内容传递给我从这个小提琴中获取的这个js:http://jsfiddle.net/abenrob/ur02w4j5/
Highcharts.setOptions({
chart: {
type: 'column',
events: {
load: function () {
var label = this.renderer.label("This text will adjust to chart resizing " +
"and redraws and will be visible on exported images.")
.css({
width: '400px',
fontSize: '9px'
})
.attr({
'r': 2,
'padding': 5
})
.add();
label.align(Highcharts.extend(label.getBBox(), {
align: 'center',
x: 0, // offset
verticalAlign: 'bottom',
y: 0 // offset
}), null, 'spacingBox');
}
},
marginBottom: 120
},
legend: {
align: 'center',
verticalAlign: 'bottom',
y: -30
},
所以我得到的不是var label = this.renderer.label("This text will adjust to chart resizing"),而是footer_caption。
我在想这样的事情:
var labelText = document.querySelectorAll('[footer_caption]').text()
var label = this.renderer.label(labelText)
但它不起作用,我什至无法从 footer_caption 获取我输入的值
【问题讨论】:
-
document.querySelectorAll('[footer_caption]')是否返回任何内容?或者这是一个空数组?无论如何,应该有:document.querySelectorAll('[footer_caption]')[0].innerText;但我担心[footer_caption]是一个错误的选择器。如果您可以展示生成的 HTML 元素的外观,那么我将能够详细介绍它。 -
查看我的编辑,我显示完整的短代码选项,它是我设置 footer_caption 的地方
-
也许我应该换个方式问:
footer_caption是在您的网站上生成的(所以您有一个标签,例如:<div footer_caption></div>)还是只是 PHP 数组中的一个元素?跨度> -
只是php数组中的一个元素
-
那么下面的答案不适合你吗?我不是 PHP 人,但我会使用:
var label = this.renderer.label(<?php echo json_encode($fields[1]['description']); ?>;).
标签: javascript php wordpress highcharts label