search

Home  >  Q&A  >  body text

javascript - How to limit the number of words entered in an input div

<p contenteditable="true"></p>

How to limit the number of characters that can be entered

为情所困为情所困2744 days ago1168

reply all(4)I'll reply

  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-17 09:59:21

    <p contenteditable="true">1</p>
    
    $ele.onkeyup = function(){
      var _html = this.innerHTML;
      if(_html.length > 10){
        this.innerHTML = _html.substr(0,10);
        this.blur();
      }
    }

    reply
    0
  • ringa_lee

    ringa_lee2017-05-17 09:59:21

    Set innerHTML.length="number of characters"

    reply
    0
  • 高洛峰

    高洛峰2017-05-17 09:59:21

    Restrict through js, refer to: http://stackoverflow.com/ques...

    reply
    0
  • 天蓬老师

    天蓬老师2017-05-17 09:59:21

      <p id="test" contenteditable="true"></p>
      <script type="text/javascript">
        document.querySelector('#test').addEventListener('keypress',function(e) {
          if(this.innerText.length>=10) {
            e.preventDefault();
          }
        });
      </script>

    reply
    0
  • Cancelreply