2012-02-04 46 views
3

所有元素這是我的HTML文件中的代碼:jQuery的影響與同名

<div id="centerBlock"> 
     <block> 
      <site style="padding-top: 10px; margin-left: 20px"> 
       <img src="./images/site1.png" alt="some_text"/> 
      </site> 
      <textOnImage style="padding-top: 10px; margin-left: 20px"> 
       lolol 
      </textOnImage> 
     </block> 

     <block> 
      <site style="padding-top: 163px; margin-left: 20px"> 
       <img src="./images/site1.png" alt="some_text"/> 
      </site> 
      <textOnImage style="padding-top: 163px; margin-left: 20px"> 
       lolol 
      </textOnImage> 
     </block> 

    </div> 

這是我的javascript:

$("block").mouseenter(function(){   
    $("site").fadeTo("slow", 0.25); 
    $("textOnImage").animate({ 
     opacity: "1" 
    }, 600); 
}); 

$("block").mouseleave(function(){   
    $("site").fadeTo("slow", 1); 
    $("textOnImage").animate({ 
     opacity: "0" 
    }, 600); 
}); 

然而,當我將鼠標懸停在這兩個塊的一個元素都是不透明的,當我將鼠標從它移開時,它們都會回到原始狀態。我搜索了幾個小時,並且我100%確定我一直在搜索不正確的條款。我如何單獨做他們?

+0

,因爲你沒有提出任何具體的div的ID,該選擇去爲所有 – kobe 2012-02-04 18:04:18

+0

我是新jquery,你能告訴我我該怎麼做? – Ridz 2012-02-04 18:05:07

+1

這個網站相當快,有很多聰明人,我正要輸入答案,但你已經得到了你要找的東西 – kobe 2012-02-04 18:06:15

回答

4

使用this僅定位觸發事件的塊,然後使用.find()在該特定塊中查找所需的元素。你之前的做法顯然是在整個頁面中找到所有site元素和所有textOnImage元素,而不僅僅是觸發事件的塊中的元素。

$("block").mouseenter(function(){   
    var $this = $(this); 
    $this.find("site").fadeTo("slow", 0.25); 
    $this.find("textOnImage").animate({ 
     opacity: "1" 
    }, 600); 
}); 

$("block").mouseleave(function(){   
    var $this = $(this); 
    $this.find("site").fadeTo("slow", 1); 
    $this.find("textOnImage").animate({ 
     opacity: "0" 
    }, 600); 
}); 
+0

@Ridz,正如jfriend所發佈的,這指的是當前對象或當前上下文。如果你在block上做mousenter,那麼這就指那個 – kobe 2012-02-04 18:07:01

+0

裏面的元素就跟其他答案一樣,它只會影響第二個'block':/ – Ridz 2012-02-04 18:11:11

+0

@Ridz - 它在這裏按預期工作:http ://jsfiddle.net/jfriend00/ZLepq/。如果它沒有在你的實際頁面中工作,那麼你的代碼/ HTML還有其他問題,你沒有向我們展示。 – jfriend00 2012-02-04 18:16:52

2

您需要遍歷元素的結構。您可以使用.find()這樣的...

$("block").mouseenter(function(){   
    $(this).find("site").fadeTo("slow", 0.25); 
    $(this).find("textOnImage").animate({ 
     opacity: "1" 
    }, 600); 
}); 

$("block").mouseleave(function(){   
    $(this).find("site").fadeTo("slow", 1); 
    $(this).find("textOnImage").animate({ 
     opacity: "0" 
    }, 600); 
}); 

在事件處理函數,this是對元素的引用到處理程序的約束。

你可以因爲有針對性的元素也使用.children()代替.find()都只有一級。


側面說明,您可以使用.hover(),並通過兩個功能這樣的..

$("block").hover(function(){   
    $(this).find("site").fadeTo("slow", 0.25); 
    $(this).find("textOnImage").animate({ 
     opacity: "1" 
    }, 600); 
}, 
    function(){   
    $(this).find("site").fadeTo("slow", 1); 
    $(this).find("textOnImage").animate({ 
     opacity: "0" 
    }, 600); 
}); 

你可能也想在你fadeToanimate呼叫添加一些.stop()電話,否則當你快速移動鼠標,動畫將被排隊備份。

+0

不,我很抱歉,但這不起作用。通過使用查找它只會影響第二個'塊',並通過使用孩子什麼都不會發生...... – Ridz 2012-02-04 18:08:40

+0

@Ridz:你是什麼意思?然後處理程序被綁定到''block''而不是''centerBlock''。 – 2012-02-04 18:10:50

+0

感謝您使代碼更加緊湊。我正要尋找能夠阻止隊列動畫的東西。但是,它只是影響第二個「塊」。我的意思是從163px開始的元素。 – Ridz 2012-02-04 18:14:51

2

試試這個(提供this上下文jQuery選擇定位的特定元素):

$("block").mouseenter(function(){   
    $("site", this).fadeTo("slow", 0.25); 
    $("textOnImage", this).animate({ 
     opacity: "1" 
    }, 600); 
}); 

$("block").mouseleave(function(){   
    $("site", this).fadeTo("slow", 1); 
    $("textOnImage", this).animate({ 
     opacity: "0" 
    }, 600); 
}); 
+0

同我不是我的答案,它只會影響第二個'塊'。 – Ridz 2012-02-04 18:09:47

+0

@Ridz:你剛纔對所有三個答案都留下了同樣的評論。它發生在你身上,因爲所有的答案都是一樣的,也許它們是相同的,這是有原因的嗎? [這裏是一個演示](http://jsfiddle.net/PpjSe/)讓你看到它的工作。 – 2012-02-04 18:14:51

+0

我刪除了位置絕對屬性的CSS文件,現在它似乎工作...尷尬,但感謝所有:) – Ridz 2012-02-04 18:17:49