Fancybox不会触发$(elem).click(),但对“真实”点击? [英] Fancybox doesn't trigger on $(elem).click(), but does on "real" click?

查看:343
本文介绍了Fancybox不会触发$(elem).click(),但对“真实”点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法获取fancybox触发。我设置它,当你点击一个元素触发。我仍然不知道为什么,但由于某种原因fancybox只有工作,如果我绑定使用 live(),排序如下: / p>

I'm having trouble getting fancybox to trigger. I have it set up to trigger when you click an element. I still don't know why, but for some reason fancybox only works if i bind it using live(), sort of like this:

$(document).ready(function() {
    // This works
    $('.fancybox').live('mousedown', function() {
        $(this).fancybox();
    });

    // This doesn't
    $('.fancybox').fancybox();
});

有关详情,请参阅:
Fancybox已加载,但没有发生任何错误,没有错误?

问题是,它只有当我实际上点击元素。如果我尝试使用以下方式触发点击:

The problem is, it only works if i actually click the element. If i try to trigger a click using:

$('.fancybox').trigger('mousedown');

它只是不工作。

我需要这样做的原因是因为图像是打印出来的PHP在画廊(缩略图等)。我只想要一个下拉的可用画廊,所以在页面加载,我隐藏所有的缩略图,然后循环通过它们,并创建一个下拉菜单包含所有的画廊。到现在为止还挺好!如果我使用检查器(使用chrome)显示元素,然后手动点击它,它工作正常!虽然,问题是,当我尝试使用下拉菜单触发点击,没有任何反应!

The reason i need to do this is because the images are being printed out with PHP in a gallery (with thumbnails etc). I only want a dropdown of the available galleries, so on page load, i hide all of the thumbnails and then loop through them and create a dropdown menu containing all the galleries. So far so good! If i show the element using the inspector (using chrome) and then manually click on it, it works fine! Although, the problem is, when i try to trigger the click using the dropdown, nothing happens!

我设置了这样每个 ;选项> 下拉菜单中的数据属性为图库/缩略图ID的数据属性,当下拉菜单发生变化时(选择选项时),它会自动查找正确的图片,并触发点击反过来应该打开一个fancybox。

I have it set up so that every <option> in the dropdown has a data attribute of the gallery/thumbnail ID, and when the dropdown changes (when you select an option), it automatically finds the correct image, and triggers a click - which in turn should open a fancybox. I know it finds the correct image, and clicks it, it's just that nothing happens when it's clicked.

尝试一下!

网址: http://goo.gl/r4Klf

右键点击内容中的第一个下拉菜单,然后检查它。在上面,你会看到一堆 .ngg-galleryoverview 元素。单击其中之一,并删除应用于元素的所有样式(在element.style中,样式基本上隐藏元素)。然后展开元素,在那里你会看到一个 .ngg-gallery-thumbnail 。从样式中删除 display:none 。您现在应该可以看到页面上的元素。如果你尝试点击它,花式框将打开。但是,如果你尝试使用 $(元素).trigger('mousedown')点击它,它不工作。尝试使用href属性定位它,例如:

Rightclick on the first dropdown menu in the content, and inspect it. Above it, you'll see a bunch of .ngg-galleryoverview elements. Click on one of them and remove all the styling applied to the element (in element.style, the style basically hides the element). Then expand the element, and in there you'll see a .ngg-gallery-thumbnail. Remove display: none from the styling. You should now be able to see the element on the page. If you try clicking it, the fancybox will open. However, if you try to click it using $(the element).trigger('mousedown'), it's not going to work. Try targeting it using the href attribute, something like:

jQuery('a[href="http://hallarna.se/wp-content/gallery/2013/spoksonatenloggaliten.jpg"]').trigger('mousedown');

请注意,jQuery是在noConflict模式,意味着你必须使用jQuery ()。

Please do note that jQuery is in noConflict mode meaning you will have to use jQuery() instead of $().

我已经战斗了几天了。任何想法是超级赞赏!

I've been battling this for several days now. Any ideas are super appreciated!

推荐答案

我想我看到的问题是。当你触发mousedown的时候,它只是触发插件使用指定的元素,当你点击它;它实际上不应该显示任何东西,直到你点击它或你触发的点击。

I think I see where the problem is. When you trigger mousedown, it just fires the plugin to work with the specified element when you click it; it's not actually supposed to show anything... until you click it or you trigger the click.

尝试类似

jQuery('a[href="http://hallarna.se/wp-content/gallery/2013/spoksonatenloggaliten.jpg"]').trigger('mousedown').trigger('click');

虽然我会寻找一个解决方案使fancybox按预期工作, ).fancybox();

Although I would look for a solution to make fancybox work as intended, with $('.fancybox').fancybox();

这篇关于Fancybox不会触发$(elem).click(),但对“真实”点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆