单击事件函数未从花式盒子灯箱内触发

Click event function not being fired from inside Fancybox lightbox

本文关键字:盒子 函数 事件 单击      更新时间:2023-09-26

我正在使用花式盒子 http://fancyapps.com/fancybox/向我的网站添加灯箱弹出窗口。我还在灯箱弹出图像下添加了一个链接,并正在尝试更改页面上的其他内容。

如果我将点击功能放入 jsfiddle 中,它可以正常工作:

http://jsfiddle.net/cmVjN/

但是当从灯箱内单击它时,不会触发点击事件功能:

        <script>
        $(document).ready(function() {
        $(".fancybox-thumb").fancybox({
             afterLoad: function() {
            this.title = '<a class="selectlink" href="#" id="' + this.title + '">Select This Image</a> ';
        },
            prevEffect  : 'none',
            nextEffect  : 'none',
            helpers : {
                title   : {
                    type: 'outside'
                },
                thumbs  : {
                    width   : 50,
                    height  : 50
                }
            }
        });

    $('.selectlink').click(function () {
        var myId = $(this.id);
        alert(this.id);
        $('#' + myId).prop('checked', true);
    });
    });
    </script>

这是html/php

echo '<div class="imagewrapper">';
echo '<a title="'.$row2['id'].'" class="fancybox-thumb" rel="fancybox-thumb" href="albums/'.$albumid.'/800x600/'.$row2['image'].'"><img style="border-radius:5px;" src="albums/'.$albumid.'/thumbnails/'.$row2['image'].'" /></a>';
  echo '<div style="text-align:center;">';
  echo '<strong>Select : </strong><input class="selectcheckbox" type="checkbox" name="'.$row2['image'].'" id="'.$row2['id'].'" />';
  echo '</div>';
  echo '</div>';

尝试:

$(document).on('click', '.selectlink', function () {
     var myId = $(this.id);
     alert(this.id);
     $('#' + myId).prop('checked', true);
});

基本上,发生的情况是设置事件处理程序时未加载元素,但是使用.on会将事件从文档委托给所需的元素。 因此,将来将添加的元素也将触发事件。

http://api.jquery.com/on/

我最终到达那里:

$(document).on('click', '.selectlink', function () {
 var myId = $('#check_' + this.id);
 $(myId).attr('checked', true);
 countChecked();