Javascript 将参数传递到此函数不起作用?
我试图构建一些可重用的代码片段,以创建打字机效果。它隐藏一个段落,然后用另一个段落替换它,其中包含一些js代码,每次打印一个字符。我试图使段落id和文本参数可以重复使用,但在允许这些参数通过我的函数时遇到了问题。到目前为止,一些非常乐于助人的人帮助了我,但我不知道这最后一步是什么 我将附加这两个函数,然后不通过参数传递。任何帮助都将不胜感激Javascript 将参数传递到此函数不起作用?,javascript,parameter-passing,Javascript,Parameter Passing,我试图构建一些可重用的代码片段,以创建打字机效果。它隐藏一个段落,然后用另一个段落替换它,其中包含一些js代码,每次打印一个字符。我试图使段落id和文本参数可以重复使用,但在允许这些参数通过我的函数时遇到了问题。到目前为止,一些非常乐于助人的人帮助了我,但我不知道这最后一步是什么 我将附加这两个函数,然后不通过参数传递。任何帮助都将不胜感激 <body> <div class="style typeClick"> <p id="remo
<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>
隐藏我
.风格{
高度:2米;
宽度:100%;
背景色:白色;
}
身体{
背景颜色:浅灰色;
}
.隐藏{
显示:无;
}
/*-------未按预期工作--------*/
(() => {
函数hideInitText(){
让hide=document.getElementById(“删除”);
hide.classList.add(“hide”);
}
hideInitText();
//创建一个参数,使每个id都能通过它
常量打字机=((id,文本)=>{
设letCounter=0;
return()=>{
让循环,类计数器;
让打字机=文本;
速度=50;
//完成后循环检查每个id
循环=document.querySelectorAll(id);
对于(classCounter=0;classCounter {
函数hideInitText(){
让hide=document.getElementById(“删除”);
hide.classList.add(“hide”);
}
hideInitText();
//创建一个参数,使每个id都能通过它
常量打字机=(()=>{
设letCounter=0;
return()=>{
让循环,类计数器;
让打字机=“打印文本”;
速度=50;
//完成后循环检查每个id
cycle=document.queryselectoral(“#type”);
对于(classCounter=0;classCounter
使用((id,text)=>{})时(
调用函数时使用零参数。如果要为该函数指定参数,请不要使用或使用(id,text)=>{((id,text)=>{})(id,text)}
const打字机=(选择器,文本)=>{
设letCounter=0;
让循环,类计数器;
让打字机=文本;
速度=50;
//完成后循环检查每个id
循环=document.querySelectorAll(选择器);
函数typeOut(){
if(letCounter<打字机长度){
对于(classCounter=0;classCounter
你是上帝派来的。不使用生命的理由是什么?没有参数仍然合适吗?根据我的理解,使用IIFE意味着创建一个函数并立即调用它。在您的例子中,您执行var tempfunc=(id,text)=>{xxxxx};var typeWriter=tempfunc()
。调用typeWriter
时,参数会给出tempfunc
返回的函数。调用typeWriter时,tempfunc返回的函数是您真正调用的。