最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - event.preventDefault() appears to not work for me within a contenteditable div - Stack Overflow

matteradmin3PV0评论

I have some simple Angular 7.x code that basically uses a contenteditable div where I am trying to prevent the default action when a user presses the [ENTER] key - the code appears fine but no matter what I seem to try it does the default action e.g moves the cursor to the next line which I am trying to prevent from happening.

What am I doing wrong?

// ponent code

onTextChange(event): void {
    // keyCode for the Enter key is 13
    if (event.keyCode === 13) {
        console.log('enterPressed');
        event.stopPropagation();
        event.preventDefault();
    }
}

// template code

<div contenteditable="true" [(ngModel)]="text" (keyup)="onTextChange($event)" (change)="onTextChange($event)" #textarea></div>

I have some simple Angular 7.x code that basically uses a contenteditable div where I am trying to prevent the default action when a user presses the [ENTER] key - the code appears fine but no matter what I seem to try it does the default action e.g moves the cursor to the next line which I am trying to prevent from happening.

What am I doing wrong?

// ponent code

onTextChange(event): void {
    // keyCode for the Enter key is 13
    if (event.keyCode === 13) {
        console.log('enterPressed');
        event.stopPropagation();
        event.preventDefault();
    }
}

// template code

<div contenteditable="true" [(ngModel)]="text" (keyup)="onTextChange($event)" (change)="onTextChange($event)" #textarea></div>
Share Improve this question asked Sep 23, 2019 at 10:50 ZabsZabs 14.2k51 gold badges180 silver badges311 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

Use keypress or keydown, which is caught at the moment user presses key, instead of "after" user has already pressed the key. Also you should get an error in console trying to use ngModel on a div. You can catch the value with $event.target.innerText instead:

onTextChange(event): void {
  // keyCode for the Enter key is 13
  if (event.keyCode === 13) {
    console.log('enterPressed', event.target.innerText);
    event.preventDefault();
  }
}

Template:

<div contenteditable="true" (keydown)="onTextChange($event)">

STACKBLITZ

In Angular, you can directly use it like (keydown.enter)="$event.preventDefault()"

<div contenteditable="true" [(ngModel)]="text" (keydown.enter)="$event.preventDefault()" #textarea></div>
Post a comment

comment list (0)

  1. No comments so far