无法在也具有 .toggle() 函数的元素上触发 FancyBox

发布于 2025-01-03 13:20:18 字数 2555 浏览 2 评论 0原文

我有一个奇怪的问题,我似乎无法弄清楚。我尝试在 SO 和其他网站上搜索,但我似乎无法解决这个愚蠢的问题。

这是我的问题 - 我正在使用 FancyBox 通过 AJAX 加载一些页面。我的那个工作得很好。

我还有一些元素使用 .toggle(); 将其类更改为“active”以更改样式。

两者单独工作都很好。问题是我有一个标志图标,它应该加载 FancyBox 实例,同时它需要向图标添加一个“active”类。这个想法是,一旦保存表单,就会应用一个标志,并且用户知道有一个与该数据位关联的标志。

这是我的 JS 代码:

            $('.activate').toggle(function() {
                 $(this).addClass('active');
            }, function() {
                 $(this).removeClass('active');
            });

            $('.activate.active').toggle(function() {
                 $(this).removeClass('active');
            }, function() {
                 $(this).addClass('active');
            });

            $('.popup').fancybox({
                 autoDimensions: true,
                 modal: true
            });

您看到的是两个单独的函数,用于打开/关闭类(某些元素将加载为活动元素,这就是为什么有两个函数)。

这是我的 HTML:

<a class="activate popup fancybox.ajax icon flag" href="flag-rock.aspx">text</a>

“active”类加载切换功能,“popup”类应该加载 FancyBox 功能,“fancybox.ajax”告诉 FancyBox 通过 AJAX 加载内容。 “图标”和“旗帜”只是简单地设置锚点的样式。

所以,这是我的交易。如果我删除“活动”FancyBox 有效,否则,我会切换该类,但不会触发 FancyBox。我在 firebug 或 Chrome 的开发工具中也没有收到任何错误。

我什至尝试了这样的事情:

            $('.activate').toggle(function() {
                $(this).fancybox({
                    autoDimensions: true,
                    modal: true
                }).addClass('active');
            }, function() {
                $(this).removeClass('active');
            });

发生的情况是图标将获得活动类,但不会触发 FancyBox。如果我再次单击它,它确实会触发 FancyBox,但只有在单击该元素两次之后才会触发。

您可以在此处查看该页面: http://ec2-50-16-99-3.compute-1.amazonaws.com/aptify/rocks/global-rocks.aspx 单击右侧的“标志”图标数据行。

有什么想法吗?

解决方案

好的,感谢下面每个人的帮助,这是适合我的解决方案。

jQuery:

        $('.popup, .active').fancybox({
                autoDimensions: true,
                modal: true
            });

            $('.activate').click(function()
            {
                $(this).toggleClass('active off');
                event.preventDefault()
            });

HTML:

Stuck Rock''

preventDefault( ) 当该标志未打开时,超链接将无法工作。否则,您将在窗口内获得传统的页面加载,这不是我们想要的。

可能有更好的方法来做到这一点,但这就是我知道如何解决打开标志时页面加载问题的全部方法。

I'm having a strange issue that I can't seem to figure out. I tried searching on SO and other sites, but I can't seem to get this stupid issue resolved.

Here's my problem - I'm using FancyBox to load some pages via AJAX. I have that working fine.

I also have some elements that are using .toggle(); to change it's class to "active" to change the styling.

Both on their own work fine. The problem is that I've got a flag icon that is supposed to load a FancyBox instance, at the same time it needs to add a class of "active" to the icon. The idea is that once the form is saved, a flag is applied and the user knows that there is a flag associated with this bit of data.

Here is my JS code:

            $('.activate').toggle(function() {
                 $(this).addClass('active');
            }, function() {
                 $(this).removeClass('active');
            });

            $('.activate.active').toggle(function() {
                 $(this).removeClass('active');
            }, function() {
                 $(this).addClass('active');
            });

            $('.popup').fancybox({
                 autoDimensions: true,
                 modal: true
            });

What you're looking at is two separate functions that toggle the class on/off (some of the elements will load as active, which why there are two functions).

Here is my HTML:

<a class="activate popup fancybox.ajax icon flag" href="flag-rock.aspx">text</a>

The class of "active" loads the toggle function, the class of "popup" is supposed to load the FancyBox function, and "fancybox.ajax" tells FancyBox to load the content via AJAX. "icon" and "flag" simply style the anchor.

So, here's my deal. If I remove "active" FancyBox works, otherwise, I get the toggle of the class, but no FancyBox firing. I'm also not getting any errors in firebug or Chrome's dev tools.

I even tried something like this:

            $('.activate').toggle(function() {
                $(this).fancybox({
                    autoDimensions: true,
                    modal: true
                }).addClass('active');
            }, function() {
                $(this).removeClass('active');
            });

What happened was that the icon would get the class of active, but not fire FancyBox. If I clicked it again, it DID fire FancyBox, but only after the element was clicked twice.

You can view the page here: http://ec2-50-16-99-3.compute-1.amazonaws.com/aptify/rocks/global-rocks.aspx Click on the "flag" icon to the right of the data rows.

Any ideas?

SOLUTION

Ok, so thanks to the help of everyone below here is the solution that works for me.

jQuery:

        $('.popup, .active').fancybox({
                autoDimensions: true,
                modal: true
            });

            $('.activate').click(function()
            {
                $(this).toggleClass('active off');
                event.preventDefault()
            });

HTML:

<a class="activate off fancybox.ajax icon flag" href="flag-rock.aspx">Stuck Rock</a>''

the preventDefault() keeps the hyperlink from working when the flag is not toggled on. Otherwise you'd get the traditional page load within the window which is not what we want.

There may be a better way to do that, but that was all I knew how to do to fix the issue of the page loading when the flag was toggled on.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

-柠檬树下少年和吉他 2025-01-10 13:20:18

我认为你应该只在 doc.ready 中应用一次 fancybox

另外,你应该查看 http://api.jquery .com/toggleClass/ 用于您的班级切换。

但我认为您真正需要的是 onStart 和 onClosed 事件来切换您的标志。这样,当页面在花式框中打开时,您的标志就会处于活动状态。

尝试这样的事情:

    $(function(){
          $('#fancything').fancybox({
               autoDimensions: true,
               modal: true
          });
          $('.activate').click(function(){
                $(this).addClass('active').removeClass('popup fancybox.ajax')
          });
    });

I think you should only apply fancybox once in the doc.ready

Also, you should look into http://api.jquery.com/toggleClass/ for your class toggling.

But I think really what you are after is onStart and onClosed events to toggle your flag. That way your flag is active when the page is open in fancy box.

Try something like this:

    $(function(){
          $('#fancything').fancybox({
               autoDimensions: true,
               modal: true
          });
          $('.activate').click(function(){
                $(this).addClass('active').removeClass('popup fancybox.ajax')
          });
    });
北音执念 2025-01-10 13:20:18

如何在 click 事件处理程序之前设置 fancybox-up 以便在第一次单击时做好准备?您在第一次单击时设置 fancybox,以便在第二次单击时准备就绪,只需在绑定 click 的同时在元素上运行 fancybox 插件即可通过 .toggle() 的事件处理程序:

        $('.activate').fancybox({
            autoDimensions: true,
            modal: true
        }).toggle(function() {
            $(this).addClass('active');
        }, function() {
            $(this).removeClass('active');
        });

我刚刚对此进行了测试,当我在页面上运行此代码时,它会在第一次单击标志图标时显示弹出窗口。

How about setting the fancybox-up before the click event handler so it's ready to go on the first click? You are setting-up the fancybox on the first click so it's ready on the second click, just run the fancybox plugin on the elements at the same time that you bind the click event handlers via .toggle():

        $('.activate').fancybox({
            autoDimensions: true,
            modal: true
        }).toggle(function() {
            $(this).addClass('active');
        }, function() {
            $(this).removeClass('active');
        });

I just tested this and when I run this code on your page it makes the popup display on the first click of the flag icons.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文