javascript - HTML Contenteditable only on one line - Stack Overflow

I have an app that has different lists a user can make, and the user can edit the name, but if he types

I have an app that has different lists a user can make, and the user can edit the name, but if he types a new name, he should be able to type enter and go many lines down.

I want to make the editable area only one line. I added a max height and overflow hidden, but that literally only hides what the other things you type, and I want only one line to stay.

code:

h3 {
  height: 25px;
  max-height: 25px;
  overflow: hidden;
}
<h3 contenteditable="true">To Do List</h3>
<section id="pageOne" contenteditable="true">
    <li style="color: #393939;"></li>
</section>

I have an app that has different lists a user can make, and the user can edit the name, but if he types a new name, he should be able to type enter and go many lines down.

I want to make the editable area only one line. I added a max height and overflow hidden, but that literally only hides what the other things you type, and I want only one line to stay.

code:

h3 {
  height: 25px;
  max-height: 25px;
  overflow: hidden;
}
<h3 contenteditable="true">To Do List</h3>
<section id="pageOne" contenteditable="true">
    <li style="color: #393939;"></li>
</section>

pictures:

Share Improve this question edited Oct 30, 2017 at 16:54 1stthomas 9272 gold badges17 silver badges27 bronze badges asked Oct 30, 2017 at 16:09 hannacreedhannacreed 6693 gold badges17 silver badges34 bronze badges 1
  • 1 You can try to add keydown listener to you editable div, and if enter key was pressed just preventDefault() that. – Taras Danyliuk Commented Oct 30, 2017 at 16:18
Add a ment  | 

1 Answer 1

Reset to default 8

As @TarasDanylyuk said, you can use keydown bined with preventDefault() to acplish that.

Here's a working example:

document.getElementById('pageOne').addEventListener('keydown', function(e) {
  if (e.keyCode == 13) {    // if Enter has been pressed
    e.preventDefault();
  }
});
h3 {
  height: 25px;
  max-height: 25px;
  overflow: hidden;
}
<h3 contenteditable="true">To Do List</h3>
<section id="pageOne" contenteditable="true">
  <li style="color: #393939;"></li>
</section>

If you want to remove focus when Enter is pressed, you can do so by adding this line after the preventDefault() call:

e.target.blur();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745604247a4635580.html

相关推荐

  • javascript - HTML Contenteditable only on one line - Stack Overflow

    I have an app that has different lists a user can make, and the user can edit the name, but if he types

    13小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信