在jquery中点击光标选择contenteditable td的全部内容

时间:2017-08-21 13:51:47

标签: jquery html

在jQuery中单击时选择的整个文本

  <tr>
  <td class="wt" type="text" contenteditable="true" align="right">0.00</td>
  </tr>

1 个答案:

答案 0 :(得分:0)

希望这会有所帮助:

$('#selectAll').on('focus', function() {
  var cell = this;
  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);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<tr>
  <td><div id="selectAll" contenteditable="true">0.00</div></td>
</tr>