3

这是我的 html(我使用 Galleria Image Gallery - 基于 JQuery)

<div id="galleria">
            <img alt="Productname 1" src="bens_img/1.jpg">
            <img alt="Productname 2" src="bens_img/2.jpg">
            <img alt="Productname 3" src="bens_img/3.jpg">
            <img alt="Guess what?" src="bens_img/4.jpg">
        </div>

伪代码

Get string from <alt> from <img>
Create a new <li> and paste the <alt> string from <img> in it

这应该使用所有(即:四个)img alt 字符串来完成。它应该如下所示:

<ul class="textformat">
<li>Productname 1</li>
<li>Productname 2</li>
<li>Productname 3</li>
<li>Guess what?</li>
</ul> <!-- yeah this was all done by java-script -->
4

5 回答 5

6

像这样:

$("#galleria > img").each(function(i, ele) {
  $(".textformat").append("<li>"+$(ele).attr("alt")+"</li>");
});
于 2010-11-22T08:09:59.077 回答
0
$("img[alt]")

标记每个具有“alt”的图像

于 2010-11-22T08:11:18.797 回答
0

您需要.attrjQuery 选择器:

$('#galleria > img').each(function(){
  $('.textformat').append('<li>'+ $(this).attr('alt') +'</li>');
});

或者您可以使用$('#galleria').find('img')$('#galleria').children('img')

于 2010-11-22T08:16:35.860 回答
0

尝试使用 jQuery 的map()函数:

$('#galleria img').map(function() {
  return '<li>' + this.alt + '</li>';
}).appendTo('ul.textformat');
于 2010-11-22T08:21:31.337 回答
0

您可以扩展 Galleria 以将 alt 标签或描述放在另一个 div 中,不知道如何将其放入 li,我猜是一些 jQuery extend: function() { this.bind(Galleria.IMAGE, function(e) { $('.caption').html(this.$('info-description').html()); }) }

于 2010-12-01T18:13:34.207 回答