Wrapping text inside input type=“text” element HTML/CSS

后端 未结 4 816
遇见更好的自我
遇见更好的自我 2020-11-30 06:11

The HTML shown below,


is displayed in a browser like so:

4条回答
  •  南方客
    南方客 (楼主)
    2020-11-30 06:49

    To create a text input in which the value under the hood is a single line string but is presented to the user in a word-wrapped format you can use the contenteditable attribute on a

    or other element:

    const el = document.querySelector('div[contenteditable]');
    
    // Get value from element on input events
    el.addEventListener('input', () => console.log(el.textContent));
    
    // Set some value
    el.textContent = 'Lorem ipsum curae magna venenatis mattis, purus luctus cubilia quisque in et, leo enim aliquam consequat.'
    div[contenteditable] {
      border: 1px solid black;
      width: 200px;
    }

提交回复
热议问题