Placeholder for contenteditable div

后端 未结 12 1552
栀梦
栀梦 2020-12-12 23:18

I have the following: FIDDLE

The placeholder works fine and dandy until you type something, ctrl + A, and delete. If you do that, th

12条回答
  •  独厮守ぢ
    2020-12-13 00:03

    var placeholderCommentText = 'Comment...', placeholderComment = $('#write_comment').attr('placeholder', placeholderCommentText); $('#write_comment').text(placeholderCommentText); $('[contenteditable]').bind({ focus: function(){ if ($('#write_comment').text().length == 0 || $('#write_comment').text() == $('#write_comment').attr('placeholder')) { $(this).empty(); } $(this).keyup(function(){ if ($('#write_comment').text() == $('#write_comment').attr('placeholder')){ $('#write_comment').attr('placeholder',''); } else if ($('#write_comment').text().length == 0 ) { $('#write_comment').attr('placeholder', placeholderCommentText); } }); }, focusout: function(){ if ($('#write_comment').text().length == 0) { $(this).text($(this).attr('placeholder')); } } });

提交回复
热议问题