存储在数组中所有img标记id

时间:2013-08-14 16:46:23

标签: jquery arrays

我正在尝试使用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>

JQUERY

$(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"));
});

3 个答案:

答案 0 :(得分:3)

使用.map()

var imglen = $('#trash li img');
var aux = imglen.map(function(){
    return this.id
})

答案 1 :(得分:0)

var imgList = $('#trash').find('img').attr('id');
$.each(imgList,function(i,v) {
    console.log(v);
});

答案 2 :(得分:0)

试试这个我们将所有图片标签的ID放入数组

  

$(function(){

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

});
  });