【问题标题】:Colorbox call with dynamically created selectors使用动态创建的选择器调用颜色框
【发布时间】:2011-07-15 08:56:44
【问题描述】:

像这样动态生成 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>

错误: 首先点击添加:

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 内容在视图中。

*SilverStripe 基于 MVC 范例。

【问题讨论】:

  • 你在用 javascript 注入 php 吗?发布更多 $("#$this 段的周边代码。

标签: php jquery-selectors colorbox


【解决方案1】:

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

页面扩展 SiteTree { }

Page_Controller 扩展 ContentController { 公共初始化(){ 要求::customScript(...); } }

因此,将 customScript 添加到您正在使用的页面类型的控制器中。那应该可以正常工作。

【讨论】:

  • 是的,这一切都是在 SomePageType::init() 中完成的,当我查看 html 源代码时,我得到了生成的 JS。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-26
  • 1970-01-01
  • 1970-01-01
  • 2015-07-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多