Javascript jQuery-contenteditable-确定插入符号是在图像之前还是之后
我不得不调试一个供应商api(wysiwyg),有一个问题,是否可以确定插入符号是在图像之前还是之后 插入图像时,会产生以下标记:Javascript jQuery-contenteditable-确定插入符号是在图像之前还是之后,javascript,jquery,redactor,Javascript,Jquery,Redactor,我不得不调试一个供应商api(wysiwyg),有一个问题,是否可以确定插入符号是在图像之前还是之后 插入图像时,会产生以下标记: <figure> <img src='/ur/face.png' /> <figcaption>Ur face</figcaption> </figure> 能否使用document.getSelection().anchorOffset 在使用进行测试时,如果克拉在图像之前,则该值为0,
<figure>
<img src='/ur/face.png' />
<figcaption>Ur face</figcaption>
</figure>
能否使用
document.getSelection().anchorOffset
在使用进行测试时,如果克拉在图像之前,则该值为
0
,如果克拉在图像之后,则该值为1
(假设克拉在这两种情况下都位于
标记内)。您是否查看该值以确定插入符号的位置?根据Redactor的演示,它实际上是一个div,具有contenteditable=true
。所以这可能会有所帮助:@RobM。是的,我在初步的谷歌搜索中看到了这一点,但是console.log($('.redactor editor').prop('selectionStart'))代码>未定义。您已锁定它。它确实在按照你所描述的那样做。我尝试使用this.selection.current().anchorOffset
的api调用,但是没有成功,但是document.getSelection().anchorOffset
成功了。谢谢
onEnter: function(e)
{
....
//-----
// !!!!! HACK !!!!!
//-----
/*
// figure
else if (this.keydown.figure)
{
setTimeout($.proxy(function()
{
this.keydown.replaceToParagraph('FIGURE');
}, this), 1);
}
*/
else if (this.keydown.figure)
{
var node = $(this.opts.emptyHtml);
// If after image (1), then we will insert after image
if (1 === document.getSelection().anchorOffset) {
$(this.keydown.figure).after(node);
}
// If before image (0), lets insert before image so users can move images down
else {
$(this.keydown.figure).before(node);
}
this.caret.start(node);
return false;
}
//-----