2013-04-28 74 views
0

我想使一个段落的文本等于图像的alt标记我正在使用此代码和jQuery的循环插件。jquery幻灯片图像标题

<html> 
<head> 
<style type="text/css">.slideshow{height:770px;width:365px;margin:auto}.slideshow img{padding:10px;border:1px solid #ccc;background-color:#000000}</style> 
<!-- include jQuery library --> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script> 
<!-- include Cycle plugin --> 
<script type="text/javascript" src="http://cloud.github.com/downloads/malsup/cycle/jquery.cycle.all.latest.js"></script> 
<script type="text/javascript">$(document).ready(function(){ 
///Added ready function to ensure code works on slower browsers 
$(document).ready(function(){$('.slideshow').cycle({fx:'fade', pause: 1});}); 
    $("#test1").text(("#slideshow2").attr("alt")); 
    }); 
}); 
</script> 
</head> 
<body> 
    <div id="slideshow2" class="slideshow"> 
    <img src="http://cloud.github.com/downloads/malsup/cycle/beach1.jpg" width="365" height="770" alt="1"/> 
    <img src="http://cloud.github.com/downloads/malsup/cycle/beach2.jpg" width="365" height="770" alt="2"/> 
    <img src="http://cloud.github.com/downloads/malsup/cycle/beach3.jpg" width="365" height="770" alt="3"/> 
    <img src="http://cloud.github.com/downloads/malsup/cycle/beach4.jpg" width="365" height="770" alt="4"/> 
    <img src="http://cloud.github.com/downloads/malsup/cycle/beach5.jpg" width="365" height="770" alt="5"/> 
</div> 
<p id=test1></p> 
</body> 
</html> 

谢谢你,提前抱歉,我所假设的是一个相当愚蠢的问题。

回答

0

你可以这样做:

$(document).ready(function(){ 
    $('.slideshow').cycle({ 
     fx:'fade', 
     pause: 1, 
     after: function() { 
      var altText = $('#slideshow2').find('img:visible').attr('alt'); 
      $('#test1').text(altText); 
     } 
    }); 
}); 
+0

非常感谢我讨厌要求更多,但你可以解释为什么工作?我的错在哪里?我想变得更好,不只是使用其他人。 – Jacob 2013-04-28 21:54:51

+0

首先,你有一个错字,你忘了在之前放上一个“$”(“#slideshow2”),其次,你尝试使用alt attr的部分是错误的,你应该从图像中取出,而不是从图像保存器中取出。但即使你确实做了正确的事情,它也不会再起作用。你试图让alt attr只有一次,但是你需要在每次新的幻灯片之后这样做,这就是回调“after”进入游戏的地方。因此,在每张幻灯片之后,我们将当前可见元素替换为属性并将其添加到#test1 div。 – drip 2013-04-29 04:41:43

+0

谢谢。对此,我真的非常感激。 – Jacob 2013-04-30 00:25:01