简体   繁体   English

如何切片动态添加到元素的文本?

[英]How to slice text dynamically added to an element?

I'm making an app where users can type messages. 我正在开发一个用户可以在其中键入消息的应用程序。 It has its own keyboard, and you click on each letter to type in the box. 它有自己的键盘,然后单击每个字母以在框中键入。 I also have a delete button, where you can click on it to delete the last letter of what you typed. 我还有一个删除按钮,您可以在其中单击以删除键入内容的最后一个字母。 However, my jQuery isn't working. 但是,我的jQuery无法正常工作。

Here's my Codepen . 这是我的Codepen

JQuery: jQuery的:

$(document).ready(function () {
var myArray = ['Q', 'W', 'E', 'R', 'T', 'Y']
$.each( myArray, function( i, item ) {
var key = "<div class='letter'>" + item + "</div>";
$(".keyboard").append(key);
});

$('.letter').click(function (){
$('#text').append($(this).text());
});

$("#delete").click(function() {
var str = $('#text').text();
str.innerHtml.slice(0,-1);
});
});

I also followed this post on modifying link elements, but their solution didn't work for me. 我还关注了这篇有关修改链接元素的帖子 ,但是它们的解决方案对我不起作用。

Can anyone help me figure out what's going on? 谁能帮我弄清楚发生了什么事? Thanks so much. 非常感谢。

You were missing on 2 parts. 您缺少2个部分。 1. str is already a string and innerHTML is not a property of string 2. After you splice it you need to set it back in html 1. str已经是一个字符串, innerHTML不是字符串的属性2.拼接后,需要将其设置回html

Hence, you need to update your delete function to following 因此,您需要将删除功能更新为

$("#delete").click(function() {
    var str = $('#text').text();
    $('#text').text(str.slice(0,-1));
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM