您可以使用jQuery腳本將textarea轉換爲div,並用單詞的跨度填充它。用邊界強調跨度。在點擊和強制焦點時將其轉換回textarea。
HTML
<textarea class="textarea-underlined">cat ; dog ; pig ; tail</textarea>
jQuery的
// Dynamically provide onBlur support for all newly added textarea elements.
$('body').on('blur','.textarea-underlined',function(e){
e.preventDefault();
// Copy textarea contents to array and split by semi-colon
var contents = $(this).val();
words = contents.split(';');
// Create div and fill with styled spans.
$(this).replaceWith('<div class="pseudo-textarea"></div>');
words.forEach(function(el,index,arr){
$('.pseudo-textarea').append('<span class="underlined">'+el.trim().toString()+'</span>');
if(words.length-1 != index){
$('.pseudo-textarea').append(";");
}
});
});
// Reverse the whole thing onClick of the div.
$('body').on('click','.pseudo-textarea',function(e){
e.preventDefault();
// Build array.
var contents = new Array();
$.each($(this).find('span'), function(index, value){
contents.push($(value).html());
});
// Replace div with textarea and fill with array contents separated by semi-colon.
$(this).replaceWith('<textarea id="textarea-underlined" class="textarea-underlined">'+contents.join(' ; ')+'</textarea>');
$('#textarea-underlined').focus();
});
CSS
.underlined {
border-bottom: 1px dashed black; // Your choice: solid, dotted, dashed...
}
.underlined:hover {
border-bottom: 1px dashed red; // Whatever color you want for hover.
}
span {
margin-right: 4px;
margin-left: 4px;
}
// Make the textarea and div look the same.
.pseudo-textarea, textarea {
cursor: text;
font-family: 'Arial';
font-size: 1em;
border: 1px solid #D4D4D4;
padding: 5px;
resize: none;
display: inline-block;
height: 100px; width: 300px;
}
.pseudo-textarea > span:first-child{
margin-left: 1px;
}
http://jsfiddle.net/X8934/5/
編輯框?是一個textarea?打字時顯示下劃線? – SajithNair
這可能是可能的,但你應該發佈一些努力.. –
嗨,它的編輯框。不需要在打字時我不需要。一旦焦點移出編輯框我希望他們得到下劃線 – Naruto