我有一個textarea:onClick textfield顯示一個div?
<textarea cols="10" rows="5">Type in the text</textarea>
我想要顯示的文本區域下方的股利(或<span>
)時的onclick上textarea的。
我怎麼能這樣做?
此外,我想隱藏它在div(或跨度)單擊鏈接時。
我有一個textarea:onClick textfield顯示一個div?
<textarea cols="10" rows="5">Type in the text</textarea>
我想要顯示的文本區域下方的股利(或<span>
)時的onclick上textarea的。
我怎麼能這樣做?
此外,我想隱藏它在div(或跨度)單擊鏈接時。
最基本的方法就是給一個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>
謝謝...這工作! :) – Akos
如果你使用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,而不是他們的標籤名,如textarea
和span
的元素 - 它將意味着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>
是跨度已經存在或者是你在飛行中產生的呢?另外,你到目前爲止還有什麼? – pimvdb
跨度已經存在。它隱藏着:style =「display:hidden;」 – Akos