<td class="box">
<span>
<img src='http://something.com' />
</span>
</td>
如何選擇span標籤? 我需要添加一個額外的圖像與jQuery(用戶腳本),但選擇跨度,然後添加元素比我想象的更難。在元素中查找沒有class或id的元素 - jQuery
<td class="box">
<span>
<img src='http://something.com' />
</span>
</td>
如何選擇span標籤? 我需要添加一個額外的圖像與jQuery(用戶腳本),但選擇跨度,然後添加元素比我想象的更難。在元素中查找沒有class或id的元素 - jQuery
任何這些應該工作
$('span', '.box');
$('.box span');
$('.box > span');
$('.box').find('span');
$('.box').children('span');
$('.box').children('span').first();
等....
甚至
$('img[src$="something.com"]').before('<img src="newimage.jpg" />');
$('span:has(img)');
$('span').filter(function() { return $(this).find('img[src$="something.com"]').length })
$('img[src="http://something.com"]').closest('span');
如果你想只針對與特定src
持有圖像,然後具體span
,您可以使用上面的選擇。
但是,如果您想要以span爲後代的所有td.box
,那麼@ adeneo的建議應該可行。
$( '盒子跨度')將讓您選擇的跨度
您可以使用jquery查找方法http://api.jquery.com/find/
只需你顯示的片段,我可能會使用這樣的:
$('td.box span')
如果有其他的家長,你可以使用它們來獲取更具體的td.box。
非常感謝。我嘗試過,但顯然我做錯了什麼。 – MortenMoulder