jQuery不想从.load中选择元素

时间:2014-04-23 19:48:25

标签: jquery

我一直试图让这项工作,但我找不到解决方案。 谢谢。

//original index.php
<div class="imgContain"> </div>

//index.php after calling load.php [$('.imgContain').load('load.php');]
<div class="imgContain">
    <img class="finalimg" alt="link" src="website.com/imga546.jpg">
    <img class="finalimg" alt="green" src="website.com/imga645.jpg">
    <img class="finalimg" alt="cap" src="website.com/imga6786.jpg">
    <img class="finalimg" alt="sarge" src="website.com/imga31234.jpg">
</div>

//jQuery code I'm trying to make work
$("img[class='finalimg']").each( function(index, element){
        alert("found");   
});

//output: zero alerts

如果所有内容都是从index.php而不是load.php加载的,那么这是工作代码的链接: working

1 个答案:

答案 0 :(得分:2)

您需要使用Document.ready并使用$("img.finalimg")代替$("img[class='finalimg']")

$(document).ready(function(){
   $("img.finalimg").each( function(index, element){
        alert("found");   
  });
})

Live Demo