当我引入新的 DOM 时,如何继承 jQuery 效果

发布于 2024-12-08 03:19:35 字数 436 浏览 10 评论 0原文

我有一个幻灯片,我替换了幻灯片中的无序列表,图像发生变化,但任何效果都不会继承。这是引入新 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 技术交流群。

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

发布评论

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

评论(3

轻许诺言 2024-12-15 03:19:35

您必须让我们知道您正在使用什么幻灯片插件。无论如何,您可能只需要销毁旧的幻灯片实例并重新启动它即可。像这样的东西:

//start slideshow on
var slideshow = $('#slideshow').cycle();
slideshow.start();
$('#button').click( function() {
   //depending on plugin api maybe stop, add and start again
   slideshow.stop();
   slideshow.addImages();
   slideshow.start();
   //or perhaps just destroy old slideshow and restart
   slideshpw = $('#slideshow').cycle();
});

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:

//start slideshow on
var slideshow = $('#slideshow').cycle();
slideshow.start();
$('#button').click( function() {
   //depending on plugin api maybe stop, add and start again
   slideshow.stop();
   slideshow.addImages();
   slideshow.start();
   //or perhaps just destroy old slideshow and restart
   slideshpw = $('#slideshow').cycle();
});
再浓的妆也掩不了殇 2024-12-15 03:19:35

要将事件附加到新添加的元素,您应该使用 live()delegate();

jQuery('.yourselector').live('click', function(){

//do something on click
});

To attach events to newly added elements you should use live() or delegate();

jQuery('.yourselector').live('click', function(){

//do something on click
});
浴红衣 2024-12-15 03:19:35

您应该学习如何使用事件委托,而不是将事件附加到每个元素。这样,您就可以在某个容器(甚至 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.

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