Placeholder for contenteditable div

后端 未结 12 1570
栀梦
栀梦 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:05

    Updating Christian Brink's answer, you could/should check for more events. You can do so by simply doing:

    // More descriptive name
    var $input = $(".placeholder");
    function clearPlaceHolder() {
      if ($input.text().length == 0) {
        $input.empty();
        }
      }
    
    // On each click
    $input.keyup(clearPlaceHolder);
    
    // Probably not needed, but just in case
    $input.click(clearPlaceHolder);
    
    // Copy/paste/cut events http://stackoverflow.com/q/17796731
    $input.bind('input', (clearPlaceHolder));
    
    // Other strange events (javascript modification of value?)
    $input.change(clearPlaceHolder);
    

    Finally, the updated JSFiddle

提交回复
热议问题