我在HTML中有这个内容可编辑的div
<div contenteditable="true" id="TextOnlyPage"></div>
Run Code Online (Sandbox Code Playgroud)
继承我的jquery代码
var rxp = new RegExp("(([0-9]+\.?[0-9]+)|([0-9]+))", "gm");
$('#TextOnlyPage').keyup(function(e){
if(e.keyCode == 13){
e.preventDefault();
$('#TextOnlyPage').children().each(function() {
if ( $(this).is("div") ) {
$(this).contents().unwrap();
}});
$('#TextOnlyPage').append("<br/>");
}
$('#TextOnlyPage').children().contents().unwrap();
var $this = $(this);
var content = $this.html();
$this.html(content.replace(rxp, "<span class='highlight'>$1</span>"));});
Run Code Online (Sandbox Code Playgroud)
问题是插入符号位置,因为当它在string.replace方法中的数字周围应用span标记时,光标将转到内容的可编辑开头div.当我按下回车键时,它也不能进入下一行.
我知道我可以通过处理它range和selection对象,但无法找到一个有用的资源,以了解这些对象是如何工作的.
请为我提供这个问题的解决方案,如果是angularjs中的解决方案会更好,或者为我提供一个资源,我可以通过工作示例了解范围和选择对象.
我想每次增加一个数字string.replace()替换一个子字符串.例如:
var string = "This is this";
var number = 0;
string.replace("is", "as");Run Code Online (Sandbox Code Playgroud)
当string.replace首先是This数字变成时1,则数字的第二个is变为2最后的this数字变为3.提前致谢...!:-)
javascript ×2
angularjs ×1
caret ×1
increment ×1
jquery ×1
numbers ×1
replace ×1
str-replace ×1