gpt4 book ai didi

javascript - 向此函数传递参数不起作用?

转载 作者:行者123 更新时间:2023-12-02 22:29:52 24 4
gpt4 key购买 nike

我正在尝试构建一些可重用的代码片段来创建打字机效果。它隐藏一个段落,然后用一些一次打印一个字符的 js 代码将其替换为另一个段落。我正在尝试创建可以重复使用的段落 ID 和文本参数,但在允许这些参数通过我的函数时遇到问题。到目前为止,一些非常有帮助的人已经帮助了我,但我无法弄清楚这最后一步。

我将附加带有和不带有参数传递的函数。任何帮助将不胜感激。

    <body>

<div class="style typeClick">
<p id="remove">Hide Me</p>
<p id="type"></p>
</div>
</body>

<style>
.style {
height: 2em;
width: 100%;
background-color: white;

}
body{
background-color: lightgrey;
}

.hide {
display: none;
}
</style>

<script>
/* ---------- DOESNT WORK AS EXPECTED ---------- */
(() => {

function hideInitText() {
let hide = document.getElementById("remove");
hide.classList.add("hide");
}
hideInitText();
//make a parameter that will take each id through it

const typeWriter = ((id, text) => {
let letCounter = 0;
return () => {
let cycle, classCounter;
let typewriter = text;
let speed = 50;

//Cycle through each id after done
cycle = document.querySelectorAll(id);

for (classCounter = 0; classCounter < cycle.length; classCounter++) {
typeOut();
}


function typeOut() {
if (letCounter < typewriter.length) {
cycle[classCounter].innerHTML += typewriter.charAt(letCounter);
letCounter++;
setTimeout(typeWriter, speed);
}
}
};
})();
document.querySelector('.typeClick').addEventListener('click', function() {typeWriter("#type", "type out text")});
})();






/* ---------- WORKS AS EXPECTED ---------- */


(() => {

function hideInitText() {
let hide = document.getElementById("remove");
hide.classList.add("hide");
}
hideInitText();
//make a parameter that will take each id through it

const typeWriter = (() => {
let letCounter = 0;
return () => {
let cycle, classCounter;
let typewriter = "Type out text";
let speed = 50;

//Cycle through each id after done
cycle = document.querySelectorAll("#type");

for (classCounter = 0; classCounter < cycle.length; classCounter++) {
typeOut();
}

function typeOut() {
if (letCounter < typewriter.length) {
cycle[classCounter].innerHTML += typewriter.charAt(letCounter);
letCounter++;
setTimeout(typeWriter, speed);
}
}
};
})();
document.querySelector('.typeClick').addEventListener('click', typeWriter());
})();


</script>

最佳答案

当使用((id, text) => {})()时,该函数以零参数调用。如果您想为此函数提供参数,请不要使用 IIFE ,或使用 (id, text) => { ((id, text) => {})(id, text) }

https://codepen.io/1010543618/pen/MWWLxmN?editors=0110

  const typeWriter = (selector, text) => {
let letCounter = 0;
let cycle, classCounter;
let typewriter = text;
let speed = 50;

//Cycle through each id after done
cycle = document.querySelectorAll(selector);

function typeOut() {
if (letCounter < typewriter.length) {
for (classCounter = 0; classCounter < cycle.length; classCounter++) {
cycle[classCounter].innerHTML += typewriter.charAt(letCounter);
letCounter++;
}
setTimeout(typeOut, speed);
}
}

typeOut();
};

关于javascript - 向此函数传递参数不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58947819/

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