簡體   English   中英

jQuery用src選擇img

[英]jQuery select img with src

我想通過src屬性選擇一個圖像(使用jQuery)。 Image位於ul和div的集合中。 ul的id是“可排序的”。

這是我的HTML:

<ul id="sortable">
  <li id="pic_0">
    <div class="sortEleWrapper">
      <div class="imgWrapper">
        <img src="/test1.jpg">
      </div>
      <input type="text" name="picText" id="picText" value="""" style="width:105px;color:#aaa" class="sortInput">
    </div>
    <input type="hidden" id="picSrc" name="picSrc" value="/test1.jpg">
  </li>
</ul>

等等

這是我的js:

if($('#sortable').find('img[src="/test1.jpg"]').length > 0){
    alert('img exists');
}else{
    alert('img doesnt exists');
}

我的問題是,他們沒有找到任何圖像。 但如果我像這樣寫js:

if($('img[src="/test1.jpg"]').length > 0){
    alert('img exists');
}else{
    alert('img doesnt exists');
}

所以他們找到了圖像。

我不確定為什么會有區別,但嘗試使用$=屬性以選擇器結束

似乎工作。

示例: http //jsfiddle.net/bTf7K/

$('#sortable').find('img[src$="/test1.jpg"]')

編輯:差異可能與獲取jQuery在不同時間使用的屬性值的方法有關。

使用本機方法:

element.getAttribute("src") // returns the actual value that was set

element.src // returns the value but with the full domain path

所以我猜jQuery在不同的時間使用這兩種方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM