【问题标题】:Select text again after execCommand executionexecCommand 执行后再次选择文本
【发布时间】:2017-12-20 16:04:00
【问题描述】:

我正在使用 jQuery 构建一个文本编辑器。

我有一个内容可编辑的div,当我点击<span id="bold-text"> 我可以更改所选文本的样式:

$('span#bold-text').mousedown(function(e){
    document.execCommand('bold', false, null);
});

在执行document.execCommand(或者可能是因为点击)后,跨度选择松散了焦点。

如何在execCommand 之后自动选择相同的文本?

【问题讨论】:

  • 试试 $("span#bold-text").focus()
  • 这行不通,因为我需要关注最后选择的文本而不是跨度。

标签: javascript jquery selection execcommand


【解决方案1】:

您可能需要将最后一个焦点项目存储在 jQuery 中(不包括跨度):

var toFocus;
$("*:not(span#bold-text)").blur(function() {
  toFocus = this;
});

然后重新聚焦:

$('span#bold-text').click(function(e){        
  document.execCommand('bold', false, null);    
  if(toFocus){
    $(toFocus).focus(); 
  }        
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-22
    • 1970-01-01
    • 1970-01-01
    • 2019-12-12
    • 2022-12-18
    • 2019-03-06
    相关资源
    最近更新 更多