2012-06-05 44 views
2

我正在使用函數has()來檢查li標記內部是否有ul。如果ul標籤是li我將添加一些CSS屬性,我的代碼效果很好,當在渲染頁面被創建的li標籤,但是當我使用Ajax和$.each()創建這些li項目has()不起作用。使用jQuery函數'has'不能處理加載的數據

jQuery代碼是:

$('.innerItems li ul li').has('ul').css({'background-image':'url(images/arrow.png)', 
             'background-position':'2px center', 
             'background-repeat':'no-repeat'}); 

回答

2

使用.find相反,.has只是爲了測試它是否有ul

$('.innerItems li ul li').find('ul').css({'background-image':'url(images/arrow.png)', 
             'background-position':'2px center', 
             'background-repeat':'no-repeat'}); 

編輯: 看來想要這個:

$('.innerItems li ul li:has(ul)').css({'background-image':'url(images/arrow.png)', 
             'background-position':'2px center', 
             'background-repeat':'no-repeat'}); 
+0

如果我使用'find',css屬性將應用於''.innerItems li ul li''的ul,但我想將css屬性應用於'.innerItems li ul li' – MajAfy

+0

@MajAfy如果你的意思是,你的代碼應該沒問題,你可以做一個演示來展示你的問題嗎? – xdazz

0

不要忘了寫這行:

$('.innerItems li ul li').has('ul').css({'background-image':'url(images/arrow.png)', 
            'background-position':'2px center', 
            'background-repeat':'no-repeat'}); 

之前$.each WHI ch創建<li>項目。

0

您可以嘗試選擇UL,然後返回到最接近的LI並應用樣式。似乎更合乎邏輯的對我說:使用.find()

$('.innerItems li ul li ul').closest('li').css(//... 

替代解決方案:

$('.innerItems li ul li').find('ul').closest('li').css(//... 

但他們將是基本相等。