繁体   English   中英

未从Fancybox灯箱内部触发Click事件功能

[英]Click event function not being fired from inside Fancybox lightbox

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

如果我将click函数放到jsfiddle中,它就可以正常工作:

http://jsfiddle.net/cmVjN/

但是从灯箱内部单击时,不会触发click事件功能:

        <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();

暂无
暂无

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

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