0

我正在尝试使用 jquery 将列表中图像的所有 id 标签存储在一个数组中。
到目前为止,我有这个:

HTML

<div id="trash" class="ui-widget-content grid_8 ui-droppable">
    <ul class="gallery ui-helper-reset">
        <li class="" style="display: block; width: 48px;">
            <img id="22" src="data:image/jpeg;base64," width="96" height="72" style="display: inline-block; height: 36px;">
        </li>
        <li class="" style="display: block; width: 48px;">
            <img id="24" src="data:image/jpeg;base64," width="96" height="72" style="display: inline-block; height: 36px;">
        </li>
        <li class="" style="display: block; width: 48px;">
            <img id="23" src="data:image/jpeg;base64," width="96" height="72" style="display: inline-block; height: 36px;">
        </li>
    </ul>
</div>
<div id="stored" style=""></div>

查询

$(document).ready(function() {
  $('#trash').click(function(){
    var imglen = $('#trash li img');
    var aux = [];
    for (var i = 0; i < imglen.length; i++) {
      aux.push(imglen[i].attr("id"));
    }
    $('#stored').text(aux.join(" "));
  });
});

它没有显示任何东西。我只需要使用下面的代码显示第一个 img id:

$('#trash').click(function(){
  $('#stored').html($('#trash li img').attr("id"));
});
4

3 回答 3

3

Use .map()

var imglen = $('#trash li img');
var aux = imglen.map(function(){
    return this.id
})
于 2013-08-14T16:48:25.550 回答
0
var imgList = $('#trash').find('img').attr('id');
$.each(imgList,function(i,v) {
    console.log(v);
});
于 2013-08-14T16:47:40.610 回答
0

try this we'll get the ids of all image tag into the array

$(function () {

    $("#trash").click(function () {
       var aux = [];
      $('#trash ul li').each(function () {
        aux.push(($(this).find("img").attr("id")));
   });
  $("#stored").text(aux.join());

});
});

于 2013-08-14T17:15:38.367 回答