2010-02-05 44 views
0

我有問題請參考下面的代碼來理解這個問題。 (我刪除「<」,然後從下面的HTML代碼「>」字符,因爲否則的HTML標記不會,所以我寫了唯一的標記名稱是可見的)如何識別由其他元素或標籤包圍的組元素?

<div> 
    <div> 
     <img /> 
     <img /> 
     <img /> 
     <img /> 
    </div> 
</div> 

我要解決以下的幫助下任務jQuery

  1. 在文檔準備出來的四個img之間的內部div只有第一個img應該是可見的,其餘的三個img應該隱藏。
  2. 現在關注焦點等特殊事件,點擊等內部div之間的四個img可見img get hide,另一個應該可見。

一些其他問題:

  1. 是jQuery是能夠識別只能通過其他標籤包圍的元素?

  2. 我也想知道如何在jQuery控制流?特別是鏈式功能。 EX的 。

    1. $(selector).fun1(val,{fun2(){ }}在上面的例子中 是GET第一和以什麼順序執行哪個功能。

    2. $(selecter).fun1().fun2().fun3() 在上面的例子中,哪個函數首先被執行,以什麼順序執行。

    3. 函數鏈中的函數按什麼順序執行?

等待你的答覆傢伙!

+2

你應該修改你原來的問題(HTTP div中的://計算器。 com/questions/2206898/how-can-i-write-jquery-for-below-problem),而不是打開一個新的。 – 2010-02-05 12:27:37

回答

1

嘗試像我這樣做here


第一張圖片(twitter)不會根據您的要求更改。受影響的唯一圖像是在具有類sample

HTML

<img src="https://s3.amazonaws.com/twitter_production/a/1265328866/images/twitter_logo_header.png"/> 

<input type="text"/> 
<input type="text"/> 
<input type="text"/> 
<input type="text"/> 

<div class="sample"> 
    <img src="http://sstatic.net/so/img/logo.png"> 
    <img src="http://static.jquery.com/files/rocker/images/logo_jquery_215x53.gif"> 
    <img src="http://cacm.acm.org/images/logo.ACM.gif"> 
    <img src="http://static03.linkedin.com/img/logos/logo_linkedin_88x22.png"> 
</div> 

的JavaScript

$(function() { 
    var textboxes = $("input:text"), //gets all the textboxes   
     images = $(".sample img"); //gets all the images 

    images.not(":first").hide(); //hide all of them except the first one 
    textboxes.each(function (i) { 
     var j = i; 
     $(this).focus(function() { 
      images.hide().eq(j).show(); 
     }); 
    }); 
}); 
+1

謝謝朋友的回覆。您的解決方案對我來說非常有用。再次感謝! – 2010-02-05 12:45:12