jQuery select img with src

前端 未结 1 1280
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-15 03:49

I want to select an image (with jQuery) by the src attribute. The Image is inside an ul and a collection of div\'s. The id of the ul is \"sortable\".

Here is my HTML

1条回答
  •  囚心锁ツ
    2020-12-15 04:40

    I'm not sure why the difference, but try using the $= attribute ends with selector.

    Seems to work.

    Example: http://jsfiddle.net/bTf7K/

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

    EDIT: The difference may have something to do with the method of getting the attribute value that jQuery uses at different times.

    Using native methods:

    element.getAttribute("src") // returns the actual value that was set
    
    element.src // returns the value but with the full domain path
    

    So I'm guessing jQuery uses both of these at different times.

    0 讨论(0)
提交回复
热议问题