jQuery 访问 class 的 SRC 的语法

jQuery syntax to access the SRC of a class

我已经尝试了一个小时或更长时间来猜测获取 jQuery 中元素属性的语法。

img src(和其他属性)位于此处:

<ul id="sortable2" class="connectedSortable ui-sortable">     
  <li class="userList">
    <div class="selectedItemId" id="15974">
      <img class="selectedItemImg" src="http://asdasd.jpg" alt="Terror" in="" resonance="" style="width:216px;height:300px">
      <div id="animeTitle">Terror in Resonance</div>
    </div>
  </li>
</ul>

属性是在用户将它们拖到列表中时在移动中生成的,因此我无法调用 "id=15974"

当用户单击按钮时,我想遍历 sortable2 列表的内容并获取子对象的属性(它们的图像 src 作为起点)。

请一些天才快速浏览一下,也许可以为我的语法指明正确的方向?

到目前为止(在许多尝试中)我坐在:

$(document).ready(function() {
  $("#button").click(function () {
    alert("Hello!");
    $('.userList .selectedItemId .selectedItemImg img').each(function() {
      alert($(this).attr('src'));
      alert("Hello2");
    });
  });
});

目前除了 "Hello1" 没有发生任何事情。我让 Hello2 进行了多次尝试,但没有找到数据。

感谢您的阅读。

您正在搜索图片中的图片,试试这个

$('.userList .selectedItemId .selectedItemImg').each(function() {
                                           //Removed img
    alert(this.src);//You can use this.src instead of .attr()
    alert("Hello2");
});

这是工作示例:

$(document).ready(function() {
  $("#button").click(function () {
        $('img.selectedItemImg').each(function() {
              alert($(this).attr('src'));
        });
  });
});

演示:http://jsbin.com/vipolologi/1/