【问题标题】:Detect current img src within div检测div中的当前img src
【发布时间】: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 的编辑。

标签: jquery image url src


【解决方案1】:

您的意思是要循环浏览每张图片吗?你可以这样做:

$('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();
    }
});

【讨论】:

  • 我想查看div中当前的img src,并根据src url显示/隐藏循环按钮
  • 在你的代码中你有一个问题。在最后一部分你有else if (imgref != 'url3'||'url4'){。你不能像这样拥有|| 'url4'。你的意思是说else if (imgref != 'url3'||imgref != 'url4'){
【解决方案2】:

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

更新:

你应该有这个条件

else if (imgref != 'url3' && imgref != 'url4')

【讨论】:

  • 这几乎可以工作了。最后一个“else if”正在寻找除 url3 或 url4 之外的任何 src 来隐藏按钮。这些按钮在其他 url 中仍然可见。
猜你喜欢
  • 2012-05-26
  • 1970-01-01
  • 2016-10-25
  • 2015-08-31
  • 2020-07-21
  • 2021-07-18
  • 1970-01-01
  • 1970-01-01
  • 2018-07-13
相关资源
最近更新 更多