Placeholder in contenteditable - focus event issue

后端 未结 11 2056
无人共我
无人共我 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条回答
  • 2020-12-07 10:45

    I found that the best way to do this is to use the placeholder attribute like usual and add a few lines of CSS.

    HTML

    <div contenteditable placeholder="I am a placeholder"></div>
    

    CSS

    [contenteditable][placeholder]:empty:before {
        content: attr(placeholder);
        color: #bababa;
    }
    

    Note: the CSS :empty selector only works if there is literally nothing in-between the opening and closing tag. This includes new lines, tabs, empty space, etc.

    Codepen

    0 讨论(0)
  • 2020-12-07 10:47

    This is not exact solution of your problem ..

    in summernote options set

    airMode:true

    placeholder works in this way.

    0 讨论(0)
  • 2020-12-07 10:48

    just use css pseudo-classes.

    span.spanclass:empty:before {content:"placeholder";}
    
    0 讨论(0)
  • 2020-12-07 10:52

    In .css

    .holder:before {
        content: attr(placeholder);
        color: lightgray;
        display: block;
        position:absolute;    
        font-family: "Campton", sans-serif;
    }
    

    in js.

    clickedOnInput:boolean = false;
    charactorCount:number = 0;
    let charCount = document.getElementsByClassName('edit-box')[0];
    
    if(charCount){
    this.charactorCount = charCount.innerText.length;
    }
    
    if(charactorCount > 0 && clickedOnInput){
    document.getElementById("MyConteditableElement").classList.add('holder');
    }
    
    if(charactorCount == 0 && !clickedOnInput){
    document.getElementById("MyConteditableElement").classList.remove('holder');
    }
    
    getContent(innerText){
      this.clickedOnInput = false;
    }
    

    In .html

    <div placeholder="Write your message.." id="MyConteditableElement" onclick="clickedOnInput = true;" contenteditable class="form-control edit-box"></div>
    

    this solution worked for me in angular project

    0 讨论(0)
  • 2020-12-07 10:53

    Here's my way:

    It uses a combination of jQuery and CSS3. Works exactly like the html5 placeholder attribute!.

    • Hides itself right away when you input the first letter
    • Shows itself again when you delete what you input into it

    HTML:

    <div class="placeholder" contenteditable="true"></div>
    

    CSS3:

    .placeholder:after {
        content: "Your placeholder"; /* this is where you assign the place holder */
        position: absolute;
        top: 10px;
        color: #a9a9a9;
    }
    

    jQuery:

    $('.placeholder').on('input', function(){
        if ($(this).text().length > 0) {
            $(this).removeClass('placeholder');
        } else {
            $(this).addClass('placeholder');
        }
    });
    

    DEMO: http://jsfiddle.net/Tomer123/D78X7/

    0 讨论(0)
提交回复
热议问题