简体   繁体   English

如何使用 JavaScript 选择字符串的最后一个字符?

[英]How do I select the last character of a string, using JavaScript?

I have a function that gets the last character of a string the user types in an input .我有一个函数可以获取用户在input键入的字符串的最后一个字符。 But how do I select that single character, using execCommand() ?但是如何使用execCommand()选择该单个字符? The goal is to copy it into a different input .目标是将其复制到不同的input

I tried element.select() , but with no results.我试过element.select() ,但没有结果。

The character to paste into the new input must be the character visible in the original input, and not the character correspondent to the keyboard key the user types, since the reason for all this is having an external JS library handling some CJK character conversion in one input , and moving the result to a different one..粘贴到新input中的字符必须是原始输入中可见的字符,而不是与用户键入的键盘键对应的字符,因为所有这些的原因是有一个外部 JS 库处理一些 CJK 字符转换input ,并将结果移动到另一个..

I am going with a copy-paste approach.我将采用复制粘贴方法。 Hence, the need to select the character.因此,需要选择字符。 But if there is a better way to achieve it, feel free to tell me about it.但如果有更好的方法来实现它,请随时告诉我。

I am open to both Vanilla JavaScript and jQuery approaches.我对 Vanilla JavaScript 和 jQuery 方法持开放态度。

This is my code:这是我的代码:

JSFiddle JSFiddle

 function copyPaste () { var i1 = document.getElementById('userInput'); var i2 = document.getElementById('input2'); var c = i1.value.substr(lol.length - 1); c.select(); document.execCommand('copy'); i2.focus(); document.execCommand('paste'); i1.focus(); }
 input { width: 255px; } button { display: block; margin: 20px 0; text-align: left; }
 <input type="text" id="userInput" placeholder="First, type something here."> <button type="button" onclick="copyPaste"();>Then, click here to copy the last character<br>of the above input into the next input.</button> <input type="text" id="input2" value="Some text...">

You should not use execCommand as it is obsolete.您不应使用execCommand因为它已过时。 Moreover, you don't need to use the clipboard to transfer a (part of a) string to another input box.此外,您不需要使用剪贴板将(a 的一部分)字符串传输到另一个输入框。 This can be done with standard string handling:这可以通过标准字符串处理来完成:

  • You can use slice(-1) to get the final character.您可以使用slice(-1)来获取最终字符。

  • I would also prefer addEventListener instead of the onclick attribute (where you had a typo also).我也更喜欢addEventListener而不是onclick属性(你也有错字)。

  • With += you can append the extracted character:使用+=您可以附加提取的字符:

 var input = document.getElementById('userInput'); var output = document.getElementById('input2'); var btn = document.querySelector('button'); btn.addEventListener("click", function () { output.value += input.value.slice(-1); });
 input { width: 255px; } button { display: block; margin: 20px 0; text-align: left; }
 <input type="text" id="userInput" placeholder="First, type something here."> <button type="button">Then, click here</button> <input type="text" id="input2" value="Some text...">

The following worked for me:以下对我有用:

html: html:

<input type="text" id="userInput" placeholder="First, type something here.">
<button type="button" onclick="copyPaste()";>Then, click here to copy the last character<br>of the above input into the next input.</button>
<input type="text" id="input2" value="Some text...">

js: js:

function copyPaste () {
  var i1 = document.getElementById('userInput');
  var i2 = document.getElementById('input2');
  var c = i1.value.slice(i1.value.length - 1);
  i2.value = c;
}

Used slice() to get the last character of the string.使用slice()获取字符串的最后一个字符。 Note I also fixed the onclick handler in your html.注意我还在你的 html 中修复了onclick处理程序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果字符串javascript的最后一个字符如何摆脱? - How do I get rid if the last character of a string javascript? 如何使用 Javascript 切/切/修剪字符串中的最后一个字符? - How do I chop/slice/trim off last character in string using Javascript? 如何使用javascript替换json字符串中的所有字符 - How do I replaceAll character from json string using javascript 如何在JavaScript中的字符串的最后两个字符之间替换一个字符? - How do I replace a character between the last occurrence of two characters in a string in JavaScript? 如何使用JavaScript替换字符串中的最后一个匹配字符 - How to replace last matched character in string using javascript 如何使用正则表达式和Javascript获取字符串的第一个和最后一个字符 - how to get the first and the last character of a string using a Regex and Javascript 如何按javascript中的最后一个数字字符对文本数组进行排序 - how do I sort a array of text by the last numberic character in javascript 如何在JavaScript中找到数组名称的最后一个字符? - How do I find the last character of an array name in JavaScript? 如何从JavaScript数组中的每个字符串中选择倒数第二个单词? - How do I select the second last word from each string in an array in JavaScript? 如何检查字符串中的字符是否是 JavaScript 中的特定字符? - How do I check whether a character in a string is a specific character in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM