简体   繁体   English

jQuery .on('click'...事件仅触发一次

[英]jQuery .on('click'… event firing only once

I've been searching for this answer for a while, but still no luck. 我一直在寻找这个答案有一段时间,但还是没有运气。

I have a dynamically generated Instagram feed with one highlight image flanked by two thumbnails (previous image and next image). 我有一个动态生成的Instagram feed,其中一个突出显示图像旁有两个缩略图(上一个图像和下一个图像)。

Take a look: http://dandelionflowershop.com/index.php/website (scroll to the bottom where it shows the Instagram feed). 看一下: http : //dandelionflowershop.com/index.php/website (滚动到底部显示Instagram feed的位置)。

If a user clicks to make the carousel move to the next image, the image to the right responds correctly by changing to the next image. 如果用户单击以使轮播移动到下一个图像,则右边的图像会通过更改为下一个图像来正确响应。 That's correct. 没错 It should do this every time, but the PROBLEM is that it only activates ONCE, rather than indefinitely. 它应该每次都执行此操作,但是问题是它仅激活一次,而不是无限期地激活。 I assume this has something to do with the $('document').ready() function, but I'm really at a loss as to how to make this work properly. 我认为这与$('document')。ready()函数有关,但是我对如何使其正常工作感到茫然。

Why does it only work once? 为什么只工作一次? How can I get it to work continuously? 如何使它连续工作?

Do I need AJAX or something more complex to achieve this? 我是否需要AJAX或更复杂的东西才能实现这一目标?

Here is my code (the JQuery is at the bottom before the closing tag.: 这是我的代码(JQuery在结束标记之前的底部。:

<!-- Instagram Area -->
            <?php $instagram_array=array(); ?>
            {exp:ig_picpuller:media_recent user_id="1"}
            <?php $instagram_array[] = '{ig_low_resolution}'; ?>
            {/exp:ig_picpuller:media_recent}
            <?php $instagram_count = 0; ?>


        <div id="instagram_wrapper">
        <?php
        $instagram_count_prev = 9;
        $instagram_count_next = 1;
        $ig_next_img = $instagram_array[$instagram_count_next];
        ?>

            <div class="instagram_label left">
                <img class="left" src="{img}logo_instagram.png" />
                Instagram<br />
                <b>dandelionflowers</b>
            </div>

            <div class="instagram_thumb_left">
                <img id="insta_prev" class="width100" src="<?php print $instagram_array[$instagram_count_prev]; ?>">
            </div>

            <div class="instagram_thumb_right right">
                <img id="insta_next" class="width100" src="<?php print $instagram_array[$instagram_count_next]; ?>">
            </div>


            <div class="instagram_frame">

                <div id="instagram_carousel" class="carousel slide carousel-fade" data-interval="false">
                <!-- Carousel items -->
                    <div class="carousel-inner">
                        {exp:ig_picpuller:media_recent user_id="1"}
                            <div class="item slides instagram_highlight">
                                <img src="{ig_standard_resolution}">
                                <div class="instagram_info">
                                    <img src="{img}heart.png" />{ig_likes}<br />
                                   <img src="{img}comment.png" />{ig_caption}
                                </div>
                            </div>
                        {/exp:ig_picpuller:media_recent}
                    </div> 

                <!-- Carousel nav -->
                <a class="carousel-control left highlight_style instagram_nav_prev" href="#instagram_carousel" data-slide="prev">&lsaquo;</a> 
                <a class="carousel-control right highlight_style instagram_nav_next" href="#instagram_carousel" data-slide="next">&rsaquo;</a>
                </div> <!-- #instagram_carousel -->

            </div> <!-- .instagram_frame -->

        </div>


       <div class="clear">
       <br /><br />
       Array Information:<br />
        <?php print_r($instagram_array); ?>
        </div>

    </div> <!-- .page_wrapper -->


<!-- Bootstrap JQuery-->
<script src="https://code.jquery.com/jquery.js"></script>
<script src="{template_root}bootstrap/js/bootstrap.min.js"></script>
<!-- Dandelion Javascript -->
<script type="text/javascript" src="{template_root}site_script.js"></script>
<script>

    $('.instagram_nav_next').on('click', function() {
        <?php $instagram_count_next++;
        $ig_next_img = $instagram_array[$instagram_count_next]; ?>
        $('#insta_next').attr('src', '<?php print $ig_next_img; ?>');
    });

</script>

I really appreciate any insight! 我真的很感谢任何见识! Thank you so much! 非常感谢!

I looked the website. 我看了看网站。 if $instagram_array is the array printed in that site, then 如果$instagram_array是该网站上打印的数组,则

you want to change this <?php print $ig_next_img; ?> 您要更改此<?php print $ig_next_img; ?> <?php print $ig_next_img; ?> to something like: <?php print $ig_next_img; ?>类似于:

var i=0;
$('.instagram_nav_next').on('click', function() {        
    <?php $instagram_count_next++;
    $ig_next_img = $instagram_array[$instagram_count_next]; ?>
    $('#insta_next').attr('src', $instagram_array[i];);
    i++;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM