在里面有图像的所有锚标签上启动fancybox

tsu*_*nka 0 jquery fancybox

我在启动fancybox时遇到问题.我想在#content块内的所有图像上使用fancybox.所有图片都有以下代码:

<a href="img.jpg">
<img title="title" src="img.jpg" alt="alt" width="225" height="169">
</a>
Run Code Online (Sandbox Code Playgroud)

我不想为链接使用特殊类(图片由用户通过tinymce管理).我试过了:

$("#content a").has('img').fancybox({
..fancybox settings
});
Run Code Online (Sandbox Code Playgroud)

而且

$("#content a img").parent('a').fancybox({
..fancybox settings
});
Run Code Online (Sandbox Code Playgroud)

没有运气.$("#content a").has('img').hide()效果很好 - 所以选择合适的元素就好了.

编辑:也许为了更好的理解:使用$("#content a")作品,但适用于所有<a>标签,这意味着像<a href="google.com">google</a>"fancyboxed" 这样的简单链接.

EDIT2: HTML代码:

<div id="content">
<p>Lots of text.
    <a href="href">
        <img src="src" alt="alt" width="225" height="169" />
    </a>
</p>
<p>another text</p>
<p>more text <a href="http://www.google.com">Simple link which should not be opened by fancybox</a></p>
</div>
Run Code Online (Sandbox Code Playgroud)

感谢任何想法.

pow*_*uoy 5

$('a').has('img') 应该返回所有a具有img元素的元素,因此它不起作用真的很奇怪.这个怎么样:

$('a').each(function () {
    if ($(this).has('img')) {
        $(this).fancybox();
    }
});
Run Code Online (Sandbox Code Playgroud)