jQuery Tablesorter,专注于 select 内容

jQuery Tablesorter, Focus to select the content

我想 select 使用 JQuery 的 Tablesorter 单元格的内容。

例如 我在输入文本中应用了 setSelectionRange 方法。 良好的自动 select 文本。

<input onClick="this.setSelectionRange(0, this.value.length)" value="Sample Text" />

我希望对 Tablesorter 单元格的内容产生同样的效果 (DIV)

<div class="" contenteditable="true">0,00</div>

如何?

如果您没有使用 editable widget for my fork of tablesorter (demo)

$('div[contenteditable]').on('focus', function() {
  var cell = this;
  // select all text in contenteditable
  // see 
  var range, selection;
  if (document.body.createTextRange) {
    range = document.body.createTextRange();
    range.moveToElementText(cell);
    range.select();
  } else if (window.getSelection) {
    selection = window.getSelection();
    range = document.createRange();
    range.selectNodeContents(cell);
    selection.removeAllRanges();
    selection.addRange(range);
  }
});

如果您使用的是可编辑小部件,则使用 editable_selectAll option


更新:此方法同样有效(demo)

也就是说,如果您的浏览器支持它 - 请参阅 caniuse & this demo

$('div[contenteditable]').on('focus', function() {
  setTimeout(function(){
    document.execCommand('selectAll', false, null);
  }, 1);
});