Chcę wybrać obraz (z jQuery) przez atrybut src. Obraz jest wewnątrz ul i kolekcji div. Identyfikator ul jest "sortable".jQuery wybierz img z src
Tu jest mój 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>
itp
i tu jest moje JS:
if($('#sortable').find('img[src="/test1.jpg"]').length > 0){
alert('img exists');
}else{
alert('img doesnt exists');
}
Moim problemem jest to, że nie znaleźliśmy żadnego obrazu. Ale jeśli napiszę js w ten sposób:
if($('img[src="/test1.jpg"]').length > 0){
alert('img exists');
}else{
alert('img doesnt exists');
}
dzięki czemu znajdują obraz.
Nie musisz dokładnie testować długości. Po prostu napisz 'if ($ ('img [src ="/test1.jpg "]'). Length) {...}' –
Ponadto, nie jestem pewien, dlaczego masz atrybuty takie jak "value =" "" " '- To nie jest prawidłowy HTML. –