jQuery tmpl 插件 - 动态内容的模板结果出现问题(bug?)

发布于 2024-11-06 13:55:48 字数 479 浏览 0 评论 0原文

我使用了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妄想挽回 2024-11-13 13:55:48

正如 aleksv 指出的,传统的 .click() 处理程序仅适用于在处理程序初始化时可以选择的元素。在这种情况下,.live()(或.delegate())处理程序将是更好的方法:

// This handler will apply to any .getPage element, even if it is created
//  after this handler declaration has been executed.
$('.getPage').live('click', function(){
    alert('clicked element with getPage class');
});

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:

// This handler will apply to any .getPage element, even if it is created
//  after this handler declaration has been executed.
$('.getPage').live('click', function(){
    alert('clicked element with getPage class');
});
第几種人 2024-11-13 13:55:48

我玩弄了你的代码,现在它可以工作了: 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文