jQuery: How to find an image by its src

前端 未结 4 909
遇见更好的自我
遇见更好的自我 2020-12-07 22:08

I need to find an img by name and src. I have been trying the following to no avail.

var s = $(\"img[src=\'/images/greendot.gif\'][name=\'BS\']\");
         


        
4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-07 23:05

    Browsers add full path to src, href and similar attributes. So even though element is defined with relative path, in DOM 'src' attribute contains domain name and full path. Using [src$="..."] syntax or specifying full domain name seem to be only options for finding images by src with jQuery.


    I might be wrong here. src property of image object reports full path, but the attribute should contain path as specified in the code. [src="relative/path"] works in the latest jQuery version, maybe it was an early version where it didn't work.

提交回复
热议问题