使用动态创建的选择器调用Colorbox

时间:2011-03-14 21:56:01

标签: php jquery-selectors colorbox

我在动态生成javascript时遇到错误:

$(document).ready(function(){
  $("#$this->AddURLSegment").colorbox();
}

HTML:

<a href="<?php echo $AddURLSegment;?>?width=500&height=500&iframe=false" id="<?php rcho $AddURLSegment;?>">Add</a>

错误: 首先点击Add:

uncaught exception: Syntax error, unrecognized expression: #

第二次:

ab.html(a.close).show is not a function
uncaught exception: Syntax error, unrecognized expression: #

和2个叠加打开,一个好,一个打破。

!当我像这样硬编码选择器(ID名称)....时,它的工作原理。

$("#add").colorbox();

这里有什么锣!?

编辑:

我在silverstripe cms中这样做

javascript(noconflict)在Controller :: init()

中生成
 Requirements::customScript('

            $j(document).ready(function(){
                $j("#'.$this->AddURLSegment.'").colorbox();
            });
');

Html的内容在View中。

* SilverStripe基于MVC范例。

1 个答案:

答案 0 :(得分:0)

您的问题是您正在尝试从Controller :: init()中访问$ this-&gt; AddURLSegment。 AddURLSegment方法在控制器类中不可用。您需要在控制器的子类中调用它。大多数SilverStripe页面如下所示:

页面扩展SiteTree { }

Page_Controller扩展ContentController {    public init(){       要求:: customScript(...);    } }

因此,将customScript添加到您正在使用的页面类型的控制器中。这应该有效。

相关问题