图像上的 jQuery 单击事件仅触发一次

标签 jquery image click live

我创建了缩略图 View 。当用户单击时,我希望在对话框中弹出真实图像。这第一次有效,但是一旦 jQuery“单击”在缩略图上触发,它就不会再次触发,除非我重新加载整个页面。我尝试重新绑定(bind)关闭对话框上的单击事件,但这没有帮助。这是我的代码:

$(document).ready(function() 
    {
        $("#tabs").tabs();
        $(".selector").tabs( {selected: 0} );
        $("img.gallery").live('click',
                function()
                {
                    var src= $(this).attr('src');
                    var popurl = src.replace("thumbs/", "");
                    PopUpImage(popurl,$(this));
                });
        // Preload animation for browser cache
        var ajaximg = new Image();
        ajaximg.src = 'images/ajax-loader.gif';
    });

    function LoadProgramsAccordion()
    {
        $(function() {
            $("#programsaccordion").accordion({
                            autoHeight: false,
                navigation: true,
                            collapsible: true
            });
        });

        $(function() {
            $("#programsaccordioninner").accordion({
                            autoHeight: false,
                navigation: true,
                            collapsible: true
            });
        });

        $(function() {
            $("#policiesaccordioninner").accordion({
                            autoHeight: false,
                navigation: true,
                            collapsible: true
            });
        });

        $(function() {
            $("#registrationaccordioninner").accordion({
                            autoHeight: false,
                navigation: true,
                            collapsible: true
            });
        });

        $('.accordion .head').click(function() {
                        $(this).next().toggle();
                        return false;
                }).next().hide();
    }

    function LoadGalleryView()
    {
        $('img.gallery').each(function(){
            $(this).hover(function(){
                $(this).attr('style', 'height: 100%; width: 100%;');
            }, function(){
                $(this).removeAttr('style');
            });
        });
    }

    function CheckImage(img,html,source)
    {
        if ( img.complete )
        {
            $('#galleryProgress').html('');
            var imgwidth = img.width+35;
            var imgheight = img.height+65;
            $('<div id="viewImage" title="View"></div>').html(html).dialog(
                    {
                        bgiframe: true,
                        autoOpen: true,
                        modal: true,
                        width: imgwidth,
                        height: imgheight,
                        position: 'center',
                        closeOnEscape: true
                    });
        }
        else
        {
            $('#galleryProgress').html('<img src="images/ajax-loader.gif" /><br /><br />');
            setTimeout(function(){CheckImage(img,html);},50);
        }
    }

    function PopUpImage(url,source)
    {
        var html = '<img src="'+url+'" />';
        var img = new Image();
        img.src = url;
        if ( ! img.complete )
        {
            setTimeout(function(){CheckImage(img,html,source);},10);
        }
    }

PopUpImage() 仅在第一次单击图像时执行,我不知道如何重新绑定(bind)。

最佳答案

好的,首先,重构 LoadProgramsAccordion。


  function LoadProgramsAccordion()
    {
        $(function() {
            $("#programsaccordion, #programsaccordioninner, #policiesaccordioninner, #registrationaccordioninner").accordion({
                            autoHeight: false,
                navigation: true,
                            collapsible: true
            });
        });

         $('.accordion .head').click(function() {
                        $(this).next().toggle();
                        return false;
                }).next().hide();
    }
<code></code>
<code> <p>Second, <code>$(function(){ ...</code> works the same as <code>$(document).ready(){ function(){...,</code> keep that in mind.</p> <p><code>$(document).ready()</code> only fires one time - right after the page loads. So if you are calling <code>LoadProgramsAccordion</code> multiple times, the inner stuff is only being executed one time.</p> <p>Next, beware of <code>$(this)</code>, it can change unexpectedly. So this code</p> </code><pre><code> function() { var src= $(this).attr('src'); var popurl = src.replace("thumbs/", ""); PopUpImage(popurl,$(this)); }); </code></pre> <p>should be changed to look like this:</p>
           function()
            {
                var that = $(this);
                var src= that.attr('src');
                var popurl = src.replace("thumbs/", "");
                PopUpImage(popurl,that);
            });

接下来,我需要查看PopUpImage。您需要做很多工作才能找出问题所在。

关于图像上的 jQuery 单击事件仅触发一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2678727/

相关文章:

javascript - 两个几乎相等的 jQuery 函数;一种可以使用 IE,另一种则不能

javascript - 如何检测字符串 `article/page/inc/3/4` 在 `inc` 的最后第三个索引中有 `/` ?

html - 重复背景图像作为标题

javascript - 图像替换时出错

php - JavaScript 对象共享局部变量

jquery - Chrome 忽略 select2 的自动完成 ="off"

html - 使用 Jinja2 在 html 文件中嵌入 png 图像

javascript - 使用 jQuery 单击操作后重新启动功能

python - PyQt4 没有将我重定向到下一页

javascript - 检测到双击事件时需要取消点击/鼠标弹起事件