Placeholder in contenteditable - focus event issue

后端 未结 11 2091
无人共我
无人共我 2020-12-07 10:05

I have been trying to ask this before, without any luck of explaining/proving a working example where the bug happens. So here is another try:

I’m trying to replicat

11条回答
  •  Happy的楠姐
    2020-12-07 10:29

    I've just published a plugin for this.

    It uses a combination of CSS3 and JavaScript to show the placeholder without adding to the content of the div:

    HTML:

    CSS:

    div[data-placeholder]:not(:focus):not([data-div-placeholder-content]):before {
        content: attr(data-placeholder);
        float: left;
        margin-left: 5px;
        color: gray;
    }
    

    JS:

    (function ($) {
        $('div[data-placeholder]').on('keydown keypress input', function() {
            if (this.textContent) {
                this.dataset.divPlaceholderContent = 'true';
            }
            else {
                delete(this.dataset.divPlaceholderContent);
            }
        });
    })(jQuery);
    

    And that's it.

提交回复
热议问题