在contenteditable="true"的编辑框内使用shift+enter换行之后,结构混乱 怎么解决 求
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#editable {
width: 300px;
height: 300px;
border: 1px solid #ccc;
padding: 10px;
}
</style>
</head>
<body>
<div id="editable" contenteditable="true" oninput="inputText()" onkeydown="keyDown(event)">
</div>
<script>
function keyDown(event) {
document.execCommand('formatblock', false, '<p>');
}
function inputText() {
// 把一段文字用 p 标签包裹起来
if (document.getSelection().focusNode.data !== undefined) {
// 当前所在元素
var currentElement = document.getSelection().focusNode.parentNode;
// 父元素内容
console.log(currentElement.innerHTML);
}
}
</script>
</body>
</html>
试了一下,这样可以