2013-04-28 75 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