2011-10-12 64 views
0

首先,我有:一個簡單的div,這是onClick切換到textarea。 textarea的佔位符是div之前的值。用textarea編輯 - >如何保留佔位符?

<div class="class" id="test">Test</div> 

text = $('#test').text(); 
$('#test').replaceWith('<textarea onkeyup="update_textarea(this)" 
id="test" placeholder="' + text + '" />'); 

的updateTextarea功能使得新插入的文本可作爲價值(意味着我可以在以後再次使用插入的文本)。

現在什麼Iam丟失或我想要什麼:當我點擊我的股市價值測試,它將是可編輯的(因爲它不再是一個div,它是一個textarea)。問題是,佔位符是正確的,但是當我點擊textarea內部插入一個新文本時,佔位符被刪除,textarea是空的。我怎麼能阻止這個,把佔位符作爲一個價值在textarea?

必須是富可視的東西我認爲......但如何保持它。

謝謝

+0

這是'placeholder'應該如何工作,它是一種視覺提示,取出當用戶導航到外地。如果您想在該字段中使用可編輯的值,請改爲使用'value'屬性。 – Spudley

回答

1

一個佔位符屬性旨在消失,一旦用戶輸入文本到input,所以它的工作按預期在這種情況下$('#test').replace...

$('#test').one('focus',function(){ 
     $(this).text(text) 
    }) 

檢查添加此。我懷疑你想要將文字應用於textarea元素;並且,對於這一點,我建議如下:

$('#test').live('click', 
    function(){ 
     $(this).replaceWith('<textarea>' + $(this).text() + '</textarea>'); 
    }); 

$('textarea').live('blur', 
    function(){ 
     $(this).replaceWith('<div id="test" class="class">' + $(this).text() + '</div>'); 
    }); 

JS Fiddle demo

參考文獻:

1

入住這

$("#test").live('click',function(){ 
    var text = $('#test').text(); 
    //var text = $('#test').html(); // use this for html tags also 
     $('#test').replaceWith('<textarea id="test2">' + text + '</textarea>'); 
}) 
$("#test2").live('blur',function(){ 
     var text2 = $('#test2').val(); 
     $(this).replaceWith('<div id="test">' + text2 + '</div>'); 
}); 
    <div id="test">Test</div> 

入住的jsfiddle

http://jsfiddle.net/VbSV5/