【问题标题】:Click event function not being fired from inside Fancybox lightbox没有从 Fancybox 灯箱内部触发单击事件函数
【发布时间】:2013-09-23 21:22:34
【问题描述】:

我正在使用 fancybox 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>';

【问题讨论】:

  • 委托事件$(this).on('click','.selectlink',function () {...});这里的'this'指的是文档

标签: javascript jquery html css


【解决方案1】:

试试:

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

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

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

【讨论】:

  • 谢谢!,现在提醒我,但它仍然没有选中复选框
  • 顺便说一句,复选框与链接共享相同的ID
  • 同一页面上不能有两个具有相同 id 的元素。如果需要,您可以在 #chkbox_myid 或类似的东西之前附加某种字符串。
  • 好的,我已将复选框 ID 更改为 ';
  • and jquery to : $('#check_' + myId).prop('checked', true);
【解决方案2】:

我最终到达了那里:

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

【讨论】:

    猜你喜欢
    • 2011-03-03
    • 1970-01-01
    • 1970-01-01
    • 2020-03-16
    • 1970-01-01
    • 2013-01-27
    • 1970-01-01
    • 1970-01-01
    • 2011-07-29
    相关资源
    最近更新 更多