jQuery tmpl 插件 - 动态内容的模板结果出现问题(bug?)
我使用了 jQuery 模板 (http://api.jquery.com/jquery.tmpl/)基于远程内容在页面上呈现列表,这一切都运行良好,但是当我将类应用于内容并为类动态模板内容提供单击事件时,它不会遵循硬编码内容工作的操作美好的。
我稍微编辑了 tmpl 插件附带的基本演示,以添加静态列表作为概念证明: http ://jsfiddle.net/3eVrR/2/
单击静态“栏”链接会生成一个警报,正如我所期望的那样。单击任何按钮(卡通或戏剧)生成列表,然后单击它们的超链接(它与硬编码链接具有相同的类别)不会触发警报。
谁能看出问题是什么吗? Firebug 没有显示任何错误,经检查静态和动态内容的类是相同的。
谢谢
I've use jQuery templates (http://api.jquery.com/jquery.tmpl/) to render lists on pages based on remote content, this is all working well, however when I apply a class to the content and have a click event for the class dynamic template content doesn't respect the action where as hard coded content works fine.
I've slightly edited the basic demo which ships with the tmpl plugin to add a static list as a proof of concept: http://jsfiddle.net/3eVrR/2/
Clicking the static 'bar' link generates an alert as I'd expect. Clicking any of the buttons (Cartoons or Drama) to generate a list then clicking they hyperlink (it has the same class as the hard coded link) does not fire the alert.
Can anyone see what the problem is? Firebug does not show any errors, on inspection the classes for both the static and dynamic content is the same.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 aleksv 指出的,传统的 .click() 处理程序仅适用于在处理程序初始化时可以选择的元素。在这种情况下,
.live()
(或.delegate()
)处理程序将是更好的方法:As aleksv pointed out, traditional
.click()
handlers only apply to elements which can be selected when the handler is initialized. In this case, a.live()
(or.delegate()
) handler would be a better approach:我玩弄了你的代码,现在它可以工作了: http://jsfiddle.net/3eVrR/5/ .当所有内容加载完毕后,您必须初始化事件。您在没有电影条目时初始化了事件。然后你添加了电影条目,而 JS 并不知道它们在那里。
I played around with your code and now it works: http://jsfiddle.net/3eVrR/5/ . You have to initialize events when all the content is loaded. You initialized the events when there were no movie entries. Then you added the movie entries and JS didn't know they were there.