我有以下代码:
<script type="text/javascript" >
$(document).ready(function(){
$(".img").click(function(){
var address= $(this).attr("src");
$("#popup").fadeIn("slow");
$("#lightbox").attr("src",address);
});
$("#close").click(function(){
$("#popup").fadeOut("slow");
});
});
</script>
显然在字符串$(&#34; #lightbox&#34;)。attr(&#34; src&#34;,address); &#34; src&#34;,address是一个变量,应指向脚本以显示与灯箱弹出窗口中的源地址关联的图像。但它对我不起作用。我认为可能是我在图像缩略图上有一个叠加层,但是从下面的代码可以看出,叠加层位于图像类div的内部。有什么建议吗?
<div id="indexcontainer">
<div class="col col-1">
<div id="effect-1" class="effects clearfix">
<div class="img"><img src="images/photos/_DSC0048.jpg" alt="Symphony In Red - robertlunaphotography.com" onload="MM_effectGrowShrink(this, 600, '0%', '100%', false, false, true)">
<div class="overlay">
<h6> </h6>
<h6> </h6>
<h6> </h6>
<h4>"Symphony in Red"</h4>
</div>
</div>
</div>
<div id="effect-1" class="effects clearfix">
<div class="img"><img src="images/photos/_DSC0046alt.jpg" alt=""Second Thoughts"" longdesc="robertlunaphotography.com">
<div class="overlay">
<h6> </h6>
<h6> </h6>
<h6> </h6>
<h4>"Second Thoughts'</h4>
</div>
</div>
</div>
<div id="effect-1" class="effects clearfix">
<div class="img"><img src="images/photos/LNA_4072.jpg" width="375" height="250" alt=""The Angel of Oaks"" longdesc="robertlunaphotography.com" onload="MM_effectGrowShrink(this, 400, '0%', '100%', false, false, true)"></a>
<div class="overlay">
<h6> </h6>
<h6> </h6>
<h6> </h6>
<h4>"The Angel of Oaks"</h4>
</div>
</div>
</div>
<div id="effect-1" class="effects clearfix">
<div class="img"><img src="images/photos/LUN_0378.jpg" width="514" height="250" alt=""Table Rock Mountain"" longdesc="robertlunaphotography.com">
<div class="overlay">
<h6> </h6>
<h6> </h6>
<h6> </h6>
<h4>"Table Rock Mountain"</h4>
</div>
</div>
</div>
<div id="effect-1" class="effects clearfix">
<div class="img"><img src="images/photos/_DSC0092_sm.jpg" width="376" height="250" alt=""The Stranger"" longdesc="robertlunaphotography.com" onload="MM_effectGrowShrink(this, 600, '0%', '100%', false, false, true)">
<div class="overlay">
<h6> </h6>
<h6> </h6>
<h6> </h6>
<h4>"The Stranger"</h4>
</div>
</div>
</div>
<div id="effect-1" class="effects clearfix">
<div class="img"><img src="images/photos/_DSC0073.jpg" width="250" height="376" alt=""Jessica"" longdesc="robertlunaphotography.com">
<div class="overlay">
<h6> </h6>
<h6> </h6>
<h6> </h6>
<h4>"Jessica"</h4>
</div>
</div>
</div>
<div id="effect-1" class="effects clearfix">
<div class="img"><img src="images/photos/LNA_5203.jpg" width="375" height="250" alt=""Lake Junaluska"" longdesc="robertlunaphotography.com" onload="MM_effectGrowShrink(this, 600, '0%', '100%', false, false, true)">
<div class="overlay">
<h6> </h6>
<h6> </h6>
<h6> </h6>
<h4>"Lake Junaluska"</h4>
</div>
</div>
</div>
<div id="effect-1" class="effects clearfix">
<div class="img"><img src="images/photos/_DSC0204copy.jpg" width="250" height="334" alt=""Brooke"" longdesc="robertlunaphotography.com">
<div class="overlay">
<h6> </h6>
<h6> </h6>
<h6> </h6>
<h4>"Brooke"</h4>
</div>
</div>
</div>
</div>
</div>
<div id="popup">
<div id="center">
<img id="lightbox" src="images/largeones/Symphony-in-Red-Robert-Luna-Photography.com.jpg">
<img id="close" src="images/index/Close-button-icon-1005031249.png" alt="close" >
</div>
答案 0 :(得分:0)
您的点击处理程序是在类<div>
的{{1}}元素上定义的。因此,您尝试从img
元素而不是src
元素获取<div>
属性:
<img>
如果您真的需要点击处理程序位于$(".img").click(function(){
// In this function, $(this) refers to <div class="img"> (not <img src="...">)
}
元素上,请尝试以下操作:
<div>
如果您需要$(".img").click(function(){
var address= $(this).children().attr("src");
}
元素上的点击处理程序,请尝试以下方法:
<img>