2010-04-10 57 views
12

如何获得花式框以在页面上定位特定的DIV ID,而不是在fancybox中请求整个页面。 这是我的代码,所以票价Jquery FancyBox目标特定的DIV ID?

$(function() { 

$(".style_image a").live('click', function(event) { 

    $(".style_image a").find("#show_style").fancybox();  

}); 
}); 

这不工作?

我原来试过如下:

$(function() { 

    $(".style_image a").live('click', function(event) { 

     $(this.href + " #show_style").fancybox();  

    }); 
    }); 

不知道如何完成这件事,或者如果它甚至有可能?

here are the doc's

回答

7

你只需要这个选择:

$("#show_style").fancybox(); 

ID selector(因为ID应该是唯一的)只是#IDGoesHere

认为你以后被加载在某些fancybox中的内容,如果你想从href指向的页面加载<div id="content"></div>,你可以使用.load()

$(".style_image a").on('click', function(event) { 
    $('#show_style').load(this.href + " #content").fancybox();  
}); 

​​有url selector是只加载目标网页的一部分的选项。

+0

请你认为你能帮助我这个问题http://goo.gl/Yl5qIi – Axel 2015-10-10 14:33:46

24

如果你想的fancybox打开你只需要使用下面的简单步骤的某一格:

首先你需要一个链接挂钩的fancybox到:

<a href="#myDivID" id="fancyBoxLink">Click me to show this awesome div</a> 

注意DIV ID旁边锚点。

其次,你需要将在的fancybox中所示是一个DIV:

<!-- Wrap it inside a hidden div so it won't show on load --> 
<div style="display:none"> 
    <div id="myDivID"> 
     <span>Hey this is what is shown inside fancybox.</span> 
     <span>Apparently I can show all kinds of stuff here!</span> 
     <img src="../images/unicorn.png" alt="" /> 
     <input type="text" value="Add some text here" /> 
    </div> 
</div> 

;第三几个非常简单的JavaScript来摆弄这一起

<script type="text/javascript"> 
    $("a#fancyBoxLink").fancybox({ 
     'href' : '#myDivID', 
     'titleShow' : false, 
     'transitionIn' : 'elastic', 
     'transitionOut' : 'elastic' 
    }); 
</script> 
+0

如果您无法更改href值,那么使用ID打开fancybox中的div会如何?即当点击一个ID> – 2012-01-04 17:42:19

+0

的img时,在fancybox中打开div。在上面的代码中,Charlie在此处更改id'href':'#myDivID'。 – Yasir 2012-09-10 13:13:58

+0

如果我很好地理解了这个例子,它非常复杂,有100个图像,因为我需要编写100个脚本,每个脚本对应一个图像,因为它与ID一起工作。 另外,我的目标是打开奇特的图像在一个div是cca。 70%的屏幕宽度,但使用这种方法,当我打开图像时,它以100%的屏幕尺寸打开,就像原来一样... – 2017-10-24 00:17:27