我正在尝试学习如何使用 javascript 制作文本编辑器(我是 JavaScript 新手)。这里我使用“para”按钮创建一个段落,段落内会有一个引号。每次我按“enter”键时' 在该段落中,将创建一个新段落,而不是用于输入文本的新行。如何强制创建换行符而不是段落元素?此外,如果我单击引号之外,它也不允许我在段落内写入
<script>
function loads(){
idContent.document.designMode="On";
}
function para(){
var m=document.createElement("p");
m.setAttribute('style',"border:1px solid black;height:100px;width:500px;position:relative;background-color:white;");
m.setAttribute('designMode',"on");
m.setAttribute('contenteditable',"true");
var t=document.createElement("q");
t.innerHTML="quote here";
m.appendChild(t);
idContent.document.body.appendChild(m);
}
</script>
这是我的 html 代码,
<body onLoad="loads();" style="position:relative;">
<iframe id="idContent" width="600" height="280" contenteditable="true" ></iframe>
<input type="button" value="para" onClick="para();">
</body>
这是我的 css 样式,
<style>
* {
margin:0px;
padding:0px;
}
q {
color:;
}
#idContent{
background-color:pink;
}
div{
border:1px solid black;
}
</style>
最佳答案
你可以这样做:
function appendPara(e) {
if (e.keyCode == 13) { //13 is javascript key-code for Enter
.....
idContent.document.body.appendChild(m);
}
}
关于javascript - 如何强制 Enter 键在 iframe 编辑器中进行换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20795257/