【发布时间】:2014-04-12 17:03:42
【问题描述】:
我有一个包含许多图像的 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>
【问题讨论】:
-
#chart中有很多图片还是只有一张?你想要所有图片中的所有srcs,还是只想要一个? -
我有很多。它们是从单独的链接调用的。
-
我认为您可以通过添加到当前图像的某些类或属性来访问当前图像。对于当前图像,可能显示将是 bot none。这样您就可以识别当前图像。
-
最好你也可以添加你的div的标记。
-
这里是 div 的标记。我有嵌套的div。我正在尝试检测父 div id="chart" 中的主图像 (#IMG1) src。这个 src 可以是多个 url。我还更新了最后一个 else if 语句以包含 Gavin 的编辑。