回答編集履歴
2
ちょうせい
answer
CHANGED
@@ -18,6 +18,7 @@
|
|
18
18
|
const v=t.value;
|
19
19
|
const s=t.selectionStart;
|
20
20
|
if(e.keyCode == 13){
|
21
|
+
e.preventDefault();
|
21
22
|
t.value=v.substring(0,s)+"\n"+v.substring(s);
|
22
23
|
t.selectionStart=s+1;
|
23
24
|
t.selectionEnd=s+1;
|
1
追加
answer
CHANGED
@@ -7,4 +7,21 @@
|
|
7
7
|
<input type="submit" value="send">
|
8
8
|
</form>
|
9
9
|
```
|
10
|
-
ただしformにイベントリスナーを直接書くのはおすすめしないのでjsを分離したほうがよいでしょう
|
10
|
+
ただしformにイベントリスナーを直接書くのはおすすめしないのでjsを分離したほうがよいでしょう
|
11
|
+
|
12
|
+
# 追加処理
|
13
|
+
```javascript
|
14
|
+
document.addEventListener('keydown',e=>{
|
15
|
+
const selection = getSelection();
|
16
|
+
const t=e.target;
|
17
|
+
if(t.closest('textarea')){
|
18
|
+
const v=t.value;
|
19
|
+
const s=t.selectionStart;
|
20
|
+
if(e.keyCode == 13){
|
21
|
+
t.value=v.substring(0,s)+"\n"+v.substring(s);
|
22
|
+
t.selectionStart=s+1;
|
23
|
+
t.selectionEnd=s+1;
|
24
|
+
}
|
25
|
+
}
|
26
|
+
});
|
27
|
+
```
|