2011-09-25 68 views
0

由於某種原因.add最後一行沒有按預期工作,文檔中所有的:first-child都被修改,我只想從:first-child中修改懸停的對象。我嘗試了所有我能想到的,不知道問題出在哪裏。將jquery選擇器應用到對應的懸停對象

jQuery.fn.img=function(background,param,repeat) 
    { $(this).css('background-image','url(/static/img/'+background+'.png') 
      .css('background-repeat',repeat) 
      .css('background-position',param+' top');} 

$('#sliding-door li').hover(function(e) 
{$(this).img('b1_middle_focus','left','repeat-x'); 
$(this).add(':first-child span').img('b1_left_focus','left','no-repeat');}, 

的HTML:

<ul> 
      <li><a href="/href1"><span><span>Something</span></span></a></li> 
      <li><a href="/href2"><span><span>Another thing</span></span></a></li> 
</ul> 
+0

'.add()'?你不是指'.find()'? – BoltClock

+0

謝謝,它的工作 – titus

+0

好吧,我發佈它作爲答案。 – BoltClock

回答

1

更新

我看你想現在要做什麼。如果您還需要懸停功能適用於所有的li s左右,而測試的第一個孩子只是一個子程序,你可以使用一個if語句:

$('#sliding-door li').hover(function(e) { 
    $(this).img('b1_middle_focus','left','repeat-x'); 

    if ($(this).is(':first-child')) { 
     $(this).find('span').img('b1_left_focus','left','no-repeat'); 
    } 
}, ...); 

老答案,忽視

如果你正在尋找的$(this)的第一個孩子span,你想用$(this).find()而非$(this).add()

$(this).find(':first-child span').img('b1_left_focus','left','no-repeat'); 

.add()將文檔級別的:first-child span選擇器所匹配的所有元素添加到$(this)對象,這不像您期望的那樣。查看jQuery文檔:http://api.jquery.com/add

+0

實際上它不好,我希望它測試如果懸停的對象是第一個孩子,在那之後不選擇第一個孩子 – titus

+0

這與你所問的不同。我可以看到你的HTML嗎? – BoltClock

+0

這裏是一個例子,這兩個工作,我想第二個行爲像第一個http://pastebin.com/hCB1vWSa – titus