简体   繁体   English

在“Facebox”框中找到元素

[英]Locating an element in a 'Facebox' box

Heres my link: 继承人我的链接:

http://tinyurl.com/6j727e http://tinyurl.com/6j727e

If you click on the link in test.php, it opens in a modal box which is using the jquery 'facebox' script. 如果单击test.php中的链接,它将在模式框中打开,该框使用jquery'facebox'脚本。

I'm trying to act upon a click event in this box, and if you view source of test.php you'll see where I'm trying to loacte the link within the modal box. 我正在尝试对此框中的点击事件采取行动,如果你查看test.php的来源,你会看到我试图在模态框中找到链接。

    $('#facebox .hero-link').click(alert('click!'));

However, it doesn't detect a click and oddly enough the click event runs when the page loads. 但是,它不会检测到点击,奇怪的是,当页面加载时,click事件会运行。

The close button DOES however have a click event built in that closes the box, and I suspect my home-grown click event is being prevented somehow, but I can't figure it out. 然而,关闭按钮的内置点击事件会关闭盒子,我怀疑我的本土点击事件是以某种方式被阻止的,但我无法弄明白。

Can anyone help? 有人可以帮忙吗? Typically its the very last part of a project and its holding me up, as is always the way ;) 通常情况下,它是项目的最后一部分,它始终如一地保持着我;)

First, the reason you're getting the alert on document load is because the #click method takes a function as an argument. 首先,您获取文档加载警报的原因是#click方法将函数作为参数。 Instead, you passed it the return value of alert , which immediately shows the alert dialog and returns null. 相反,您传递了alert的返回值,它立即显示警告对话框并返回null。

The reason the event binding isn't working is because at the time of document load, #facebox .hero-link does not yet exist. 事件绑定不起作用的原因是因为在文档加载时, #facebox .hero-link尚不存在。 I think you have two options that will help you fix this. 我认为你有两个选项可以帮助你解决这个问题。

Option 1) Bind the click event only after the facebox is revealed. 选项1)仅在显示facebox后才绑定click事件。 Something like: 就像是:

$(document).bind('reveal.facebox', function() {
  $('#facebox .hero-link').click(function() { alert('click!'); });
});

Option 2) Look into using the jQuery Live Query Plugin 选项2)研究使用jQuery Live Query插件

Live Query utilizes the power of jQuery selectors by binding events or firing callbacks for matched elements auto-magically, even after the page has been loaded and the DOM updated. Live Query利用jQuery选择器的强大功能,通过绑定事件或自动神奇地触发匹配元素的回调,即使在页面加载和DOM更新后也是如此。

jQuery Live Query will automatically bind the click event when it recognizes that Facebox modified the DOM. 当jQuery Live Query识别到Facebox修改了DOM时,它将自动绑定click事件。 You should then only need to write this: 那么你应该只写这个:

$('#facebox .hero-link').click(function() { alert('click!'); });

Alternatively use event delegation 或者使用事件委托

This basically hooks events to containers rather than every element and queries the event.target in the container event. 这基本上将事件挂钩到容器而不是每个元素,并在容器事件中查询event.target。

It has multiple benefits in that you reduce the code noise (no need to rebind) it also is easier on browser memory (less events bound in the dom) 它有多种好处,因为你可以减少代码噪音(无需重新绑定),它在浏览器内存上也更容易(在dom中绑定的事件更少)

Quick example here 这里的简单例子

jQuery plugin for easy event delegation 用于轻松事件委派的jQuery插件

PS event delegation is pencilled to be in the next release (1.3) coming very soon. PS事件委托书很快就会出现在下一个版本(1.3)中。

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

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