【发布时间】:2017-09-17 19:38:59
【问题描述】:
我正在使用 Hashedit (contenteditable),当我在可编辑的文本正文(段落元素)上按 Enter 键时,它会附加一个新的段落元素。但是,我希望新的段落元素也具有与我按 Enter 键相同的类。
【问题讨论】:
标签: javascript jquery mutation-observers
我正在使用 Hashedit (contenteditable),当我在可编辑的文本正文(段落元素)上按 Enter 键时,它会附加一个新的段落元素。但是,我希望新的段落元素也具有与我按 Enter 键相同的类。
【问题讨论】:
标签: javascript jquery mutation-observers
function init() {
$("#my-div").on("keydown",function(e) {
if (e.keyCode==13) {
//Do your thing when enter is pressed
setTimeout('$("p:last").addClass("cool-class")',10);
}
});
}
.cool-class {
background-color: #f00;
color: #fff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<body onload="init()">
<div contentEditable id="my-div">
<p>Bla Bla Bla</p>
<p>Bla bla</p>
</div>
</body>
使用 jQuery。
【讨论】: