[英]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:这是我的代码:
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.