我正在使用jquery,並且正在尋找動態添加rel屬性的標記,但僅當它是child時纔是用於添加rel =「lightbox」的圖像。像下面這樣......如果孩子是img,則將rel屬性應用到<a> - jquery
if('a').children(img){
$(this).attr('rel', 'lightbox');
}
任何幫助/建議是非常感謝。
我正在使用jquery,並且正在尋找動態添加rel屬性的標記,但僅當它是child時纔是用於添加rel =「lightbox」的圖像。像下面這樣......如果孩子是img,則將rel屬性應用到<a> - jquery
if('a').children(img){
$(this).attr('rel', 'lightbox');
}
任何幫助/建議是非常感謝。
你可以使用jQuery的子選擇器。子選擇符匹配父匹配的子項。二者必選其一space
或>
分隔sub-選擇:
// find all "img" children of "a"
$('a > img').each(function(){ $(this).attr('rel', 'lightbox'); });
試試這個:
$('a > img').parent().attr('rel', 'lightbox');
$( 'IMG')父( 'A')ATTR( '相對', '收藏夾');
也許這樣的事情?不確定 !
$('a').find('img').each(function(i,v){
$(v).parent().attr('rel','lightbox');
});
我喜歡find()超過'>',因爲它快得多。
+1,但考慮使用``a> img``來匹配直接的孩子,因爲你使用的是parent()。 – 2011-02-16 09:42:26