当我引入新的 DOM 时,如何继承 jQuery 效果
我有一个幻灯片,我替换了幻灯片中的无序列表,图像发生变化,但任何效果都不会继承。这是引入新 DOM 的脚本:
jQuery("#kwick1").click(function () {
jQuery('#photography').load('/design.html #photography');
});
jQuery("#kwick2").click(function () {
jQuery('#photography').load('/design.html #design');
});
我如何让 jQuery 幻灯片继承这个新的图像列表? 我将这些功能和幻灯片放映功能都放在同一个文件中。 我有一个
jQuery(document).ready(function() {
加载幻灯片脚本的。
I have a slideshow that i replace an unordered list within the slide show, the images change but any effects are not inherited. this is the script that introduces a new DOM:
jQuery("#kwick1").click(function () {
jQuery('#photography').load('/design.html #photography');
});
jQuery("#kwick2").click(function () {
jQuery('#photography').load('/design.html #design');
});
how do i get the jQuery slideshow to inherit this new list of images??
i both these functions and the slide show function in the same file.
I have a
jQuery(document).ready(function() {
that loads the slideshow script.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须让我们知道您正在使用什么幻灯片插件。无论如何,您可能只需要销毁旧的幻灯片实例并重新启动它即可。像这样的东西:
You have to let us know what slideshow plugin you are using. Regardless, you probably just need to destroy your old slideshow instance and restart it. Something like:
要将事件附加到新添加的元素,您应该使用 live() 或 delegate();
To attach events to newly added elements you should use live() or delegate();
您应该学习如何使用事件委托,而不是将事件附加到每个元素。这样,您就可以在某个容器(甚至
document
)上附加单个事件侦听器,并在事件冒泡时捕获它们。此外,它将使您的代码看起来更干净、更容易理解。
PS,如果您将代码放在 HTML 的底部(在
之前)然后,当它触发时,它已经是一个“onDomReady”事件。
Instead of attaching events to each of elements, you should learn how to use event delegation. That way you can attach single event listener on some container ( or even
document
) and catch the events as they bubble up.Additionally it will make your code look much cleaner and easier to understand.
P.S. , if you put your code at the bottom of the HTML ( before
</body>
) then, when it triggers , it will already be an "onDomReady" event.