2011-09-08 97 views
0

我有一個textarea:onClick textfield顯示一個div?

<textarea cols="10" rows="5">Type in the text</textarea> 

我想要顯示的文本區域下方的股利(或<span>)時的onclick上textarea的。

我怎麼能這樣做?

此外,我想隱藏它在div(或跨度)單擊鏈接時。

+1

是跨度已經存在或者是你在飛行中產生的呢?另外,你到目前爲止還有什麼? – pimvdb

+0

跨度已經存在。它隱藏着:style =「display:hidden;」 – Akos

回答

5

最基本的方法就是給一個ID的跨度,然後:

<textarea cols="10" rows="5" onclick="document.getElementById('box').style.display='inline';">Type your text here</textarea> 
<span id="box" style="display:none">display</span> 
+0

謝謝...這工作! :) – Akos

3

如果你使用jQuery那麼它很簡單:

$("textarea").bind("focus", function(){ 
    $("span").show(); 
}); 

隨後的鏈接給它一個ID在HTML:

<span> 
    <a href="#" id="closeme">Close me</a> 
</span> 

然後:

$("#closeme").bind("click", function(){ 
    $("span").hide(); 
}); 

記住Javascript必須進入<script></script>標籤,並確保您在頁面中包含jQuery:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> 

如果你是新來的jQuery那麼下面這段代碼應該給你如何開始的想法。一般來說,最好還是指使用ID,而不是他們的標籤名,如textareaspan的元素 - 它將意味着JavaScript就瞄準正確的元素..像這樣的東西會做你問什麼:

<html lang="en"> 
<body> 

    <textarea id="form-details"></textarea> 
    <span id="form-details-info"> 
     Some info about the textarea 
     <br/> 
     <a href="#">Close text area</a> 
    </span> 

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>  
    <script> 
     $(function(){ 

      // When a user is using the textarea 
      $("#form-details").bind("focus", function(e){ 
       // Show the span info 
       $("#form-details-info").show(); 
      }); 

      // When a user clicks the close link 
      $("#form-details-info a").bind("click", function(e){e){ 

       // Hide the info 
       $("#form-details-info").hide(); 

       // And use this to stop a prevent a link from doing what it normally does.. 
       e.preventDefault(); 
      }); 

     }); 
    </script> 
</body> 
</html> 
+0

儘管我同意jQuery可以無縫工作,但問題並未被標記爲這樣。不過,我想,'當在div中點擊一個鏈接時,'意味着'div a'是一個選擇器。 – pimvdb

+0

@pimvdb是的,我知道 - 儘管如此,我不禁爲(看似)初學者提倡jQuery。 – betamax

+0

+1爲一個「簡單」的解決方案,佔據了頁面的大部分 –