开发者

Why is fancybox not triggering?

开发者 https://www.devze.com 2023-03-16 01:14 出处:网络
<script type=\"text/javascript\"> jQuery(document).ready(function() { jQuery(\'.excerpt_read-more\').click(function(e) {
<script type="text/javascript">
        jQuery(document).ready(function() {
            jQuery('.excerpt_read-more').click(function(e) {
                e.preventDefault();

                var url = jQuery(this).attr('href');

                jQuery.get(url, function(data) {
                    var content = jQuery(data).find('#the_content').text();

                    jQuery(this).parent().fancybox({
                        'content' : content
                    });
                });
            });
        });
        </script>

Here is my code, everything works fine up to the part 开发者_StackOverflowwhere fancybox is supposed to fire. I put the parent method because usually it would fire one-up on the .excerpt_read-more, but in any case it does not work without it.

Any suggestions?


You have a scoping/context issue. You cannot reuse this inside the callback function, because that's a whole new context. Try storing the reference to the element in a separate variable and then use that one instead. E.g.

jQuery(document).ready(function() {
        jQuery('.excerpt_read-more').click(function(e) {
            e.preventDefault();

            var url = jQuery(this).attr('href');
            var $link = jQuery(this);  // <-- see this

            jQuery.get(url, function(data) {
                var content = jQuery(data).find('#the_content').text();

                $link.parent().fancybox({  // <-- and use it here
                    'content' : content
                });
            });
        });
    });
0

精彩评论

暂无评论...
验证码 换一张
取 消