Placeholder for contenteditable div

后端 未结 12 1551
栀梦
栀梦 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-12 23:58

    This solution worked for me. I'd converted this solution from angular to pure javaScript

    In .html

    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;
    }
    

提交回复
热议问题