<div id="inputTxtArea" contenteditable="true"></div>
$(document).keydown(function (e) {
var e = e || window.event, ec = e.keyCode || e.which;
if (!e.ctrlKey && 13 == ec) {
//console.log('发送');
sendMsgTal();
return false;
}
if (e.ctrlKey && 13 == ec) {
//console.log('换行');
if (browserType() == "IE" || browserType() == "Edge") {
$("#inputTxtArea").append("<div></div>");
}
else if (browserType() == "FF") {
$("#inputTxtArea").append("<br/><br/>");
} else {
$("#inputTxtArea").append("<div><br/></div>");
}
//设置输入焦点
var o = document.getElementById("inputTxtArea").lastChild;
var textbox = document.getElementById('inputTxtArea');
var sel = window.getSelection();
var range = document.createRange();
range.selectNodeContents(textbox);
range.collapse(false);
range.setEndAfter(o);//
range.setStartAfter(o);//
sel.removeAllRanges();
sel.addRange(range);
}
});
function browserType () {
var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
var isOpera = false;
if (userAgent.indexOf('Edge') > -1) {
return "Edge";
}
if (userAgent.indexOf('.NET') > -1) {
return "IE";
}
if (userAgent.indexOf("Opera") > -1 || userAgent.indexOf("OPR") > -1) {
isOpera = true;
return "Opera"
}; //判断是否Opera浏览器
if (userAgent.indexOf("Firefox") > -1) {
return "FF";
} //判断是否Firefox浏览器
if (userAgent.indexOf("Chrome") > -1) {
return "Chrome";
}
if (userAgent.indexOf("Safari") > -1) {
return "Safari";
} //判断是否Safari浏览器
if (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera) {
return "IE";
}; //判断是否IE浏览器
}
js按ctrl+enter换行,让可编辑的div换行,并获取输入焦点光标
本文转载:CSDN博客