后退和前进按钮未按预期运行。按钮循环正确,但仅使用按钮时会将文本放在末尾。使用键盘可以吗

问题描述 投票:0回答:1

当您仅使用按钮从胡萝卜位置放置字符时,此代码行为是意外的。它总是将字符放在字符串的末尾。 如果我使用按钮并将字符放在键盘上,它就会按预期工作。 为什么会发生这种情况,非常感谢任何有关解决方案的帮助。

function getInputSelection(el) {
  var start = 0,
    end = 0,
    normalizedValue, range,
    textInputRange, len, endRange;
  if (typeof el.selectionStart == "number" && typeof el.selectionEnd == "number") {
    start = el.selectionStart;
    end = el.selectionEnd;
  } else {
    range = document.selection.createRange();
    if (range && range.parentElement() == el) {
      len = el.value.length;
      normalizedValue = el.value.replace(/\r\n/g, "\n");
      textInputRange = el.createTextRange();
      textInputRange.moveToBookmark(range.getBookmark());
      endRange = el.createTextRange();
      endRange.collapse(false);
      if (textInputRange.compareEndPoints("StartToEnd", endRange) > -1) {
        start = end = len;
      } else {
        start = -textInputRange.moveStart("character", -len);
        start += normalizedValue.slice(0, start).split("\n").length - 1;
        if (textInputRange.compareEndPoints("EndToEnd", endRange) > -1) {
          end = len;
        } else {
          end = -textInputRange.moveEnd("character", -len);
          end += normalizedValue.slice(0, end).split("\n").length - 1;
        }
      }
    }
  }
  return {
    start: start,
    end: end
  };
}

function setBack() {
  document.getElementById('user').value =
    document.getElementById('user').value.substring(0,
      document.getElementById('user').value.length - 1);
}


function moveBack(elId) {
  var elem = document.getElementById(elId);
  elem.setSelectionRange(elem.value.length,
    getInputSelection(elem).start - 1);
  elem.focus();
}

function moveForward(elId) {
  var elem = document.getElementById(elId);
  elem.setSelectionRange(elem.value.length,
    getInputSelection(elem).start + 1);
  elem.focus();
}
function focusElem(){
var foo =document.getElementById('user');
foo.focus();
}

function setA(){ document.getElementById('user').value += "A"; focusElem();}
function setB(){ document.getElementById('user').value += "B"; focusElem();}
function setC(){ document.getElementById('user').value += "C"; focusElem();}
function setD(){ document.getElementById('user').value += "D"; focusElem();}
<input id="user" type="text">
<button type="button" onclick="setBack();">backspace</button>
<button type="button" onclick="moveBack('user')">back</button>
<button type="button" onclick="moveForward('user')">forward</button>


<div>
<button type="button" onclick="setA()">A</button>
<button type="button" onclick="setB()">B</button>
<button type="button" onclick="setC()">C</button>
<button type="button" onclick="setD()">D</button>
<div>
感谢您提前抽出时间!

javascript html dom text
1个回答
0
投票

您的辅助函数不尊重插入符位置

这是一个使用委托和数据属性来没有内联事件处理程序的版本

function insertCharAtCaret(char) {
  var elem = document.getElementById("user");
  var selection = getInputSelection(elem);
  var startPos = selection.start;
  var endPos = selection.end;

  elem.value = elem.value.substring(0, startPos) + char + elem.value.substring(endPos);

  // Set the caret position after the inserted character
  elem.setSelectionRange(startPos + 1, startPos + 1);
  elem.focus();
}

function setBack() {
  var elem = document.getElementById('user');
  elem.value = elem.value.substring(0, elem.value.length - 1);
}

function moveBack() {
  var elem = document.getElementById('user');
  var start = getInputSelection(elem).start - 1;
  elem.setSelectionRange(start, start);
  elem.focus();
}

function moveForward() {
  var elem = document.getElementById('user');
  var start = getInputSelection(elem).start + 1;
  elem.setSelectionRange(start, start);
  elem.focus();
}

// Delegation for character buttons
document.getElementById("buttonContainer").addEventListener("click", function(event) {
  if (event.target.tagName === "BUTTON" && event.target.dataset.char) {
    insertCharAtCaret(event.target.dataset.char);
  }
});

const actions = {
  setBack,
  moveBack,
  moveForward,
};

document.getElementById("controlContainer").addEventListener("click", function(event) {
  const tgt = event.target.closest('button');
  if (!tgt || !tgt.dataset.action) return; // not a button or no action specified

  const actionFunc = actions[tgt.dataset.action];
  if (actionFunc) {
    actionFunc();
  }
});

function getInputSelection(el) {
  var start = 0,
    end = 0,
    normalizedValue, range, textInputRange, len, endRange;
  if (typeof el.selectionStart == "number" && typeof el.selectionEnd == "number") {
    start = el.selectionStart;
    end = el.selectionEnd;
  } else {
    range = document.selection.createRange();
    if (range && range.parentElement() == el) {
      len = el.value.length;
      normalizedValue = el.value.replace(/\r\n/g, "\n");
      textInputRange = el.createTextRange();
      textInputRange.moveToBookmark(range.getBookmark());
      endRange = el.createTextRange();
      endRange.collapse(false);
      if (textInputRange.compareEndPoints("StartToEnd", endRange) > -1) {
        start = end = len;
      } else {
        start = -textInputRange.moveStart("character", -len);
        start += normalizedValue.slice(0, start).split("\n").length - 1;
        if (textInputRange.compareEndPoints("EndToEnd", endRange) > -1) {
          end = len;
        } else {
          end = -textInputRange.moveEnd("character", -len);
          end += normalizedValue.slice(0, end).split("\n").length - 1;
        }
      }
    }
  }
  return {
    start: start,
    end: end
  };
}
<input id="user" type="text">

<div id="controlContainer">
  <button type="button" data-action="setBack">backspace</button>
  <button type="button" data-action="moveBack">back</button>
  <button type="button" data-action="moveForward">forward</button>
</div>

<div id="buttonContainer">
  <button type="button" data-char="A">A</button>
  <button type="button" data-char="B">B</button>
  <button type="button" data-char="C">C</button>
  <button type="button" data-char="D">D</button>
</div>

© www.soinside.com 2019 - 2024. All rights reserved.