我正在嘗試使用類front
獲取所有圖像並顯示它們的src屬性。看着它的控制檯它工作,但它返回的圖像與類的前面,還有圖像的類與整個img代碼。我只想要src屬性。我怎麼能做到這一點?按類名查找所有圖像並返回src屬性
HTML
<div id="results"></div>
<div id="mm_grid">
<!-- Grid contents written dynamically -->
<div class="mm_row">
<div class="mm_window" id="tile0" onclick="flipImage(this)"><img class="front" src="public/images/mm_image5.jpg" alt="" /><img class="back" src="public/images/mm_back.jpg" alt="" /></div>
<div class="mm_window" id="tile1" onclick="flipImage(this)"><img class="front" src="public/images/mm_image5.jpg" alt="" /><img class="back" src="public/images/mm_back.jpg" alt="" /></div>
<div class="mm_clearfix"></div>
</div>
<div class="mm_row">
<div class="mm_window" id="tile2" onclick="flipImage(this)"><img class="front" src="public/images/mm_image8.jpg" alt="" /><img class="back" src="public/images/mm_back.jpg" alt="" /></div>
<div class="mm_window" id="tile3" onclick="flipImage(this)"><img class="front" src="public/images/mm_image0.jpg" alt="" /><img class="back" src="public/images/mm_back.jpg" alt="" /></div>
<div class="mm_clearfix"></div>
</div>
<div class="mm_row">
<div class="mm_window" id="tile4" onclick="flipImage(this)"><img class="front" src="public/images/mm_image3.jpg" alt="" /><img class="back" src="public/images/mm_back.jpg" alt="" /></div>
<div class="mm_window" id="tile5" onclick="flipImage(this)"><img class="front" src="public/images/mm_image2.jpg" alt="" /><img class="back" src="public/images/mm_back.jpg" alt="" /></div>
<div class="mm_clearfix"></div>
</div>
</div>
的jQuery
var linkArray = $("img.front").map(function() {
return $(this).parent().html();
}).get();
console.log(linkArray);
結果
["<img class="front" src="public/images/mm_image5.jpg" alt=""><img class="back" src="public/images/mm_back.jpg" alt="">", "<img class="front" src="public/images/mm_image5.jpg" alt=""><img class="back" src="public/images/mm_back.jpg" alt="">", "<img class="front" src="public/images/mm_image8.jpg" alt=""><img class="back" src="public/images/mm_back.jpg" alt="">", "<img class="front" src="public/images/mm_image0.jpg" alt=""><img class="back" src="public/images/mm_back.jpg" alt="">", "<img class="front" src="public/images/mm_image3.jpg" alt=""><img class="back" src="public/images/mm_back.jpg" alt="">", "<img class="front" src="public/images/mm_image2.jpg" alt=""><img class="back" src="public/images/mm_back.jpg" alt="">"]
使用索引參數有點浪費代碼。你可以簡單地寫'alert($(this).attr(「src」));'這不會要求jQuery返回並找到與你的選擇器匹配的所有元素,只需選擇一個已經傳遞給回調函數。 – iMoses 2012-04-11 08:40:34
感謝您的評論:) – 2012-04-11 09:28:12