简体   繁体   English

选中复选框时如何将字符串添加到数组?

[英]How can I add a string to an array when a checkbox is checked?

These are my strings and their speeds at which they will be displayed这些是我的字符串及其显示速度

var typed = new Typed(".auto-input", {
        strings: ["Hi, blahblahblah.", "blahblahblah...", "...blahblahblah?"],
        typeSpeed: 75,
        backSpeed: 75,
        loop: true
    });

This is my code for when my checkbox is checked这是我的复选框被选中时的代码

var checkbox = document.getElementById('checkbox');

checkbox.addEventListener('change', () => {
  document.body.classList.toggle('dark');
});

I tried an if statement我试过 if 语句

if (document.body.classList.toggle('dark')) {
  typed.push(["lorem"]);
}

But then i realized, i think i need to select the "strings" in my "typed" vaiable.但后来我意识到,我想我需要 select 我的“类型”变量中的“字符串”。 I'm not quite sure how to do that.我不太确定该怎么做。 I apologize in advance for any mistakes, this is my first question posted on StackOverflow.对于任何错误,我提前道歉,这是我在 StackOverflow 上发布的第一个问题。

As you want to push "lorem" into strings inside Typed , your if statement should look like:当您想将"lorem"推入Typed内的strings时,您的 if 语句应如下所示:

if (document.body.classList.toggle('dark')) {
  typed.strings.push("lorem");
}

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

相关问题 在jQuery中选中复选框时,如何将项添加到列表中? - How can I add items to a list when a checkbox is checked in jQuery? 选中复选框时如何将元素添加到数组 - How to add an element to an array when a checkbox is checked 选中另一个复选框后,如何检查该复选框? [JavaScript的] - How can I check a checkbox when another checkbox is checked? [JavaScript] 我如何将选中复选框的数量添加到此jQuery / JS代码 - How can i add the numbers of checked checkbox to this jquery / js code 选中主复选框时,如何将复选框设置为在数据表列中选中 - How can I set checkbox to checked in a datatable column when a master checkbox is checked 如果选中任何复选框并在未选中任何复选框时禁用它,如何启用提交按钮? - How can I enable a submit button if any checkbox is checked and disable it when none checkbox is checked? 如何在选中Checkbox时调用javascript函数 - How can I call a javascript function when Checkbox is checked 当使用 CSS 选中复选框时,如何切换 div 的可见性? - How can I toggle the visibility of a div when a checkbox is checked with CSS? 如果选中复选框,如何才能更新文本框? - How can I get a textbox to update when a checkbox is checked? 选中复选框时的Javascript,将复选框ID添加到数组中 - Javascript when checkbox is checked, add checkbox ID to an array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM