gpt4 book ai didi

javascript - 从字符串中逐个删除字符

转载 作者:行者123 更新时间:2023-12-01 01:05:53 25 4
gpt4 key购买 nike

我一直在尝试创建一种效果,就像在我的网页中键入字符串一样。换句话说,该字符串将逐个字符地出现。所以我使用 jquery 做到了这一点并成功了。我使用的代码是这样的,

$(function() {
var string = "Coming Soon|",
stringCount = 0;

setInterval(function(){
$('.main_section_animate span').append(string[stringCount]);
stringCount += 1;
},100);
})

请注意,span 标记是空的,里面什么也没有。

现在的问题是我正在尝试向后删除字符串。我尝试使用 setIntervalreplace(string[stringCount],''),选择主要部分 span 和主要部分 span.text() code> 但它不起作用并给出了一些奇怪的结果。

我还尝试了其他一些方法,但主要是 text() 与替换的一些组合那么,有人可以帮我解决这个问题吗?

最佳答案

可以是:

let str = 'Coming Soon |';

const $span = $('.main_section_animate span');

$span.html(str);

const animation = setInterval(() => {
str = str.slice(0, -1)
$span.html(str)

!str.length && clearInterval(animation)
}, 100)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="main_section_animate">
<span></span>
</div>

关于javascript - 从字符串中逐个删除字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59811619/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com