点击事件不会触发新创建的li标签(click event does not fire on newly created li tags)
使用以下代码:
<!DOCTYPE html> <html> <head> <title>test list</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"> </script> </head> <style> li{ display:inline; } </style> <body> <input type="hidden" value="4" id="value"> <ol></ol> <button id="btn2">increase</button> <button id="btn1">show</button> <p></p> </body> <script> $("li").click(function(){ $(this).nextAll().css({"color":"red"});; }); $("#btn2").click(function(){ var text="<li> -> kkk</li>"; $("ol").append(text); }); $("#btn1").click(function(){ $("p").text($("li").length); }); </script> </html>
点击“增加”按钮后出现的任何新创建的“li”标签都不会触发绑定到点击事件的处理程序。
$("li").click(function(){ $(this).nextAll().css({"color":"red"});; });
你能告诉我为什么它不起作用吗? 是否有可能使其工作? 如果是,如何? 非常感谢你。
With the following code:
<!DOCTYPE html> <html> <head> <title>test list</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"> </script> </head> <style> li{ display:inline; } </style> <body> <input type="hidden" value="4" id="value"> <ol></ol> <button id="btn2">increase</button> <button id="btn1">show</button> <p></p> </body> <script> $("li").click(function(){ $(this).nextAll().css({"color":"red"});; }); $("#btn2").click(function(){ var text="<li> -> kkk</li>"; $("ol").append(text); }); $("#btn1").click(function(){ $("p").text($("li").length); }); </script> </html>
any newly created "li" tags that appear after clicking "increase" button, do not trigger handlers bound to the click event.
$("li").click(function(){ $(this).nextAll().css({"color":"red"});; });
Can you please tell me the reason why it's not work. And is it possible to make it work? If yes, How? Thank you very much.
原文:https://stackoverflow.com/questions/20087738
最满意答案
编写一个自定义路由类,用于路由到该控制器。
在您的路由中,您将实现检查您期望的实际控制器文件是否存在的逻辑,并定义回调。 您也可以使用路由选项数组来定义回调,这将使其更具可重用性。
Write a custom route class that you'll use for the route to that controller(s).
See the routing chapter of the manual for how to do that.
Inside your route you'll implement the logic that checks if the actual controller file you expect is present or not and define a callback. You can use the route options array to define a call back as well, this would make it more re-useable.
相关问答
更多-
编写一个自定义路由类,用于路由到该控制器。 有关如何执行此操作 ,请参阅手册的路由章节 。 在您的路由中,您将实现检查您期望的实际控制器文件是否存在的逻辑,并定义回调。 您也可以使用路由选项数组来定义回调,这将使其更具可重用性。 Write a custom route class that you'll use for the route to that controller(s). See the routing chapter of the manual for how to do that. Ins ...
-
它似乎从app -> model -> exampleModel.php vs Plugin -> Admin -> Model -> exampleModel.php ,你在这两个目录中有两个相同的类,但是不支持,我担心: 由于PHP在旧版本中缺少命名空间,因此您的插件中不能使用相同的类或相同的文件名。 即使它是两个不同的插件。 因此,使用唯一的类和文件名,可以使用插件名称为类和文件名添加前缀。 来源: http : //book.cakephp.org/2.0/en/plugins/how-to-cre ...
-
您需要在$routes->connect提及您的插件名称 Router::scope('/myFirstPlugin/index', function (RouteBuilder $routes){ $routes->connect('/', ['plugin' => 'myFirstPlugin','controller' => 'test', 'action' => 'index']); }); You need to mention your plugin name within $r ...
-
如何覆盖CakePHP插件中的模型和控制器?(How do I override the models and controllers in a plugin in CakePHP?)[2023-08-16]
不,你不能像在视图文件中那样覆盖应用程序中的任何插件类文件。 只需制作插件的副本并修改所需的类文件即可。 No you cannot override any plugin class files in your app like you can do for view files. Just make a copy of the plugin and modify required class files. -
如何检查cakephp-2.3中控制器使用的型号?(How to check which model is using by a controller in cakephp-2.3?)[2023-05-10]
您可以尝试使用get_class()来确定它是否真的使用“用户”模型,或者通用的“AppModel”或“模型”; 在你的控制器的动作内; debug(get_class($this->User)); 如果它是'Model'或'AppModel',CakePHP显然无法从插件中找到或加载您的User模型。 还要检查你是否真的在bootstrap.php中加载了插件。 阅读此处的文档: 插件 You may try get_class() to find out if it is really using t ... -
为了构建Thorpe Obazee的回答和我的评论,最好的做法是将权限管理作为控制器组件,而不是控制器本身。 如前所述,你真的不应该从控制器调用控制器 - 这违背了MVC的原则(反过来CakePHP)。 一个组件可以被封装在一个插件中。 如果你的插件被称为Permissions ,你的组件也被称为Permissions ,那么你的目录结构如下所示: 应用 插入 权限 调节器 零件 PermissionsComponent.php 当你加载你的插件时,你可以在核心应用程序的控制器中使用这个组件作为任何其他组件 ...
-
确保插件已加载到您的config / bootstrap.php中 Plugin::load('Elite'); 然后用localhost:8675 / elite / bingo.txt链接到插件的webroot Make sure the plugin is loaded in your config/bootstrap.php Plugin::load('Elite'); Then link to the webroot of the plugin with localhost:8675/elit ...
-
首先, public $helpers = array('Html'); 不会在控制器中加载帮助程序 ,它会将其加载到控制器呈现的视图文件中 。 您将能够在呈现的视图中使用$ this-> Html引用,而不是任何控制器操作。 这与您是否尝试在插件控制器或普通控制器中加载Helper无关。 检查Cake Docs中的包树,并阅读有关Component和Helper之间差异的更多信息。 您可以在Controller中使用Component ,在View中使用Helper 。 您不应该在控制器中使用HtmlHe ...
-
扩展cakephp插件(Extends cakephp plugin)[2022-05-02]
你也许可以使用组合而不是继承? 即创建一个“app \ controllers \ users_controller”,其中包含插件控制器的实例。 UsersController通过存根传递任何未修改的操作,例如: class UsersController extends AppController { ... var spark_plug_users_controller; ... public function __construct() { parent: ... -
添加'plugin'=>'PLUGIN NAME HERE'