2013-03-14 74 views
0

我已经使用灯箱和变化之前,但我很难让它正常工作。希望有人有一些建议,因为我远不是JavaScript的专家。 C#/ ASP网站有一个MasterPage,每个其他页面使用该MasterPage。链接也是通过代码动态生成的。我的测试中有两个结果:它只是像普通链接一样工作,并将我重定向到图像,或者当我点击它时什么都不做。Jquery灯箱不能使用MasterPage

在母版目:

<script src="../js/jquery-1.7.2.min.js"></script> 
<script src="../js/lightbox.js"></script> 

在页代码:

DynLink.ImageUrl = "~/PostPhotos/Thumbnails/" + Path.GetFileNameWithoutExtension(image.Photo) +  "_thumb100_100" + Path.GetExtension(image.Photo).Replace("//","/"); 
DynLink.NavigateUrl = "~/PostPhotos/Thumbnails/" + Path.GetFileNameWithoutExtension(image.Photo) + "_thumblowres" + Path.GetExtension(image.Photo); 
DynLink.Attributes.Add("rel", "lightbox"); 

(检查经由萤火虫),其被生成的链接:

<a href="PostPhotos/Thumbnails/grumpy_cat_christmas_9_thumblowres.jpg" rel="lightbox"> 
<img alt="" src="PostPhotos/Thumbnails/grumpy_cat_christmas_9_thumb100_100.jpg"> 
</a> 

该图像示出了罚款,当我用萤火虫检查它时,一切似乎都很好(它具有'rel'属性等)。

+0

Path.GetFileNameWithoutExtension Path变量应该有路径 “〜/ PostPhotos /缩略图/”,检查运行时间源,你可能会得到错误的文件路径/ SRC, – MarmiK 2013-03-14 05:58:15

+0

你初始化了灯箱吗? $(function(){// Lightbox init}); – luke2012 2013-03-14 05:59:31

+0

我不知道你必须初始化它吗?从我见过的例子中,只需引用.js文件并将rel部分添加到链接中?这就是我之前完成的所有工作,并且已经成功添加 – 2013-03-14 06:23:03

回答

0

请确保你在的document.ready在母版页某处配置收藏

$(document).ready(function(){ 

$('YourLightBoxSelector').lightbox(); 

}); 
+0

...但它没有解决任何事情=/ – 2013-03-14 06:17:59

0

因为你的元素被动态地添加...调用灯箱,动态生成的元素在文档中附加后,将工作....

DynLink.ImageUrl = "~/PostPhotos/Thumbnails/" + Path.GetFileNameWithoutExtension(image.Photo) +  "_thumb100_100" + Path.GetExtension(image.Photo).Replace("//","/"); 
DynLink.NavigateUrl = "~/PostPhotos/Thumbnails/" + Path.GetFileNameWithoutExtension(image.Photo) + "_thumblowres" + Path.GetExtension(image.Photo); 
DynLink.Attributes.Add("rel", "lightbox"); 
//your append code.. 
$('#dynamicelementID').lightbox(); 
+0

对不起,添加该行不编译,是否意味着在JavaScript或C#? – 2013-03-14 06:17:26

+0

javascript ..... – bipen 2013-03-14 06:29:16