2010-02-05 49 views
0

我需要幫助我是jQuery的新手,我想在我的一個網頁中實現jQuery。 我也有一個窗體有四個文本框。我有兩個div標籤之一,另一個即我的代碼結構是按以下方式如何爲以下問題編寫jQuery?

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

我只是想用jQuery的幫助,

  1. 在文件準備第一張圖片應該是可見出盡的四個<img>和其餘三個<img>應該隱藏在內部div標記和集中在第一個文本框上的光標之間。
  2. 現在當焦​​點聚集到第二個文本框時,當前可見的<img>應該隱藏,只有第二個<img>應該可見。
  3. 當焦點聚集到第三個文本框時,當前可見的<img>應該隱藏,只有第三個應該可見。
  4. 這樣第四個<img>應該是可見的,當控制轉到第四個文本框時。

整體而言,當焦點轉到任何文本框時,與其相關的<img>應該是可見的,其餘三個<img>應該被隱藏。

因此,請幫助做上述jQuery代碼,除了上面提到的映像外,在執行以下功能時不會影響同一頁上的其他圖像。

等待你的回覆,夥計們!

+2

,如果你把你的輸入框的HTML過這將幫助格的人。 – rochal 2010-02-05 11:46:51

+2

爲什麼不先嚐試一下自己,然後再回來一些具體的問題。你會更好地學習jQuery。 – 2010-02-05 11:47:08

+0

我在佈局中看不到任何文本框 – 2010-02-05 11:47:27

回答

2

試試像我一樣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(); 
     }); 
    }); 
}); 
+0

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

0

的HTML可能是這樣的:

<div> 
    <textarea class="first"></textarea> 
    <textarea class="second"></textarea> 
    <textarea class="third"></textarea> 
    <textarea class="forth"></textarea> 
</div> 
<div class="images"> 
    <div> 
     <img class="first" src=".." /> 
     <img class="second" src=".." /> 
     <img class="third" src=".." /> 
     <img class="forth" src=".." /> 
    </div> 
</div> 

和jQuery的:

$(document).ready(function() { 
    $(".images img").hide(); 
    $("img.first").show(); 
    $("textarea").focus(function() { 
     $(".image img").hide(); 
     $("img."+$(this).attr("class")).show(); 
    }); 
}) 

與問題將是,你不能其他類的文本域和圖像。如果你是在需要這樣做,你就必須檢查是否有某一類像

if ($(this).hasClass("first")) { 
    $("img.first").show(); 
} else if ... 
+0

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