【发布时间】:2010-10-04 12:58:33
【问题描述】:
我正在使用[drupal_add_js_()][1] 函数为节点中的某些图像添加fancybox 效果(我没有使用 Fancybox 模块,因为不符合我的需要)。
简而言之,我需要添加titleFormat函数来格式化图片标题;在我的 javascript 文件中,它看起来像:
$("a[rel=myfancy_group]").fancybox({
'transitionIn': 'elastic',
'transitionOut': 'elastic',
'titlePosition': 'over',
'titleFormat': function(title, currentArray, currentIndex, currentOpts) {
return '<span><b>' + title + '</b> | Image ' + (currentIndex + 1) + ' of ' + currentArray.length + (title.length ? ' ' + title : '') + '</span>';
}
});
这就是我的drupal_add_js 函数的外观:
drupal_add_js(
array(
'mycustom_fancybox' => array(
'selector' => 'div.field-field-immagini-minigallery a',
'group' => TRUE,
'options' => array(
'transitionIn' => 'elastic',
'transitionOut' => 'elastic',
'titlePosition' => 'inside',
'titleShow' => TRUE,
'width' => 500,
'cyclic' => TRUE,
'titleFormat' => ???
)
)
),
'setting',
'footer',
FALSE,
TRUE,
TRUE
);
编辑我试过了:
//add fancybox settings
drupal_add_js(
array(
'mycustom_fancybox' => array(
'selector' => 'div.field-field-immagini-minigallery a',
'group' => TRUE,
'options' => array(
'transitionIn' => 'elastic',
'transitionOut' => 'elastic',
'titlePosition' => 'inside',
'titleShow' => TRUE,
'width' => 500,
'cyclic' => TRUE,
'titleFormat' => "function my_title_format(title, currentArray, currentIndex, currentOpts) { return '<span><b><i>' + title + '</i></b> | Immagine ' + (currentIndex + 1) + ' di ' + currentArray.length + (title.length ? ' ' + title : '') + '</span>'; }"
)
)
),
'setting',
'footer',
FALSE,
TRUE,
TRUE
);
但是(如我所料)Drupal 将其渲染为:
"function (title, currentArray, currentIndex, currentOpts) { return \'\x3cspan\x3e\x3cb\x3e\x3ci\x3e\' + title + \'\x3c/i\x3e\x3c/b\x3e | Immagine \' + (currentIndex + 1) + \' di \' + currentArray.length + (title.length ? \' \x26nbsp; \' + title : \'\') + \'\x3c/span\x3e\'; }"
...它不起作用。
我试过了
drupal_add_js(
array(
'mycustom_fancybox' => array(
'selector' => 'div.field-field-immagini-minigallery a',
'group' => TRUE,
'options' => array(
'transitionIn' => 'elastic',
'transitionOut' => 'elastic',
'titlePosition' => 'inside',
'titleShow' => TRUE,
'width' => 500,
'cyclic' => TRUE,
'titleFormat' => 'my_title_format'
)
)
),
'setting',
'footer',
FALSE,
TRUE,
TRUE
);
//and, in my js file, added:
function my_title_format(title, currentArray, currentIndex, currentOpts) {
return '<span><b><i>' + title + '</i></b> | Immagine ' + (currentIndex + 1) + ' di ' + currentArray.length + (title.length ? ' ' + title : '') + '</span>';
}
但还是不行。
【问题讨论】:
-
我不清楚这个问题的总体目标。您想添加
titleFormat却无法正常工作? -
@NoParrots:我想在 Drupal.settings 中添加 titleFormat 选项,但我不知道该怎么做,因为 titleFormat 不是字符串/布尔值,需要一个函数回调。
-
您是否尝试过仅在选项数组中传递函数回调?它有效吗?如果不是,将函数定义用单引号括起来怎么样?
-
为什么需要在 Drupal.settings 中添加它?那不是放置javascript代码的地方,只有设置。你不能让它工作吗,例如将你拥有的第一段代码添加为“内联”?
标签: php javascript jquery drupal fancybox