检测div中的当前img src

时间:2014-03-09 17:58:44

标签: jquery image url src

我有一个包含许多图像的div(id =“chart”)。如何检测div中的img src并为其分配var。这是我到目前为止所尝试的内容。

$(document).ready(function() {
    $('#Stopbutton').hide();

    var imgref = $('img','#chart').attr('src');

    if (imgref == 'url1'){
        $('#Loopbutton').show();
    }
    else if (imgref  == 'url2'){
        $('#Stopbutton').show();
    }
    else if (imgref != 'url3'||imgref != 'url4'){
        $('#Stopbutton').hide();
        $('#Loopbutton').hide();
    }

    //add more jquery here to keep inside the document ready function
});    




<div id="chart">      


<img id="IMG1" name="mymap" src="url1"; width=1000; height=700;"/>    

<a id="Loop" title="Loop Image">  
  <img id="Loopbutton" src="loopimage.png" width="132" height="22"></a>

  <a id="Stop" title="Stop Loop">
  <img id="Stopbutton" src="stoploop.png" width="112" height="22"></a>


<div class="basemap" id="ME_basemap" >
<img src="other url" width="1000" height="700"></div>

</div>

2 个答案:

答案 0 :(得分:0)

您的意思是想要遍历每张图片吗?你会这样做:

$('img','#chart').each(function(){
    $('#Stopbutton').hide();

    var imgref = $(this).attr('src');

    if (imgref == 'url1'){
        $('#Loopbutton').show();
    }
    else if (imgref  == 'url2'){
        $('#Stopbutton').show();
    }
    else if (imgref != 'url3' || imgref != 'url4'){
        $('#Stopbutton').hide();
        $('#Loopbutton').hide();
    }
});

答案 1 :(得分:0)

var imgref = $('#chart img')。attr('src');

更新:

可能你应该有条件

else if (imgref != 'url3' && imgref != 'url4')
相关问题