简体   繁体   English

当我调用 function 时,如何在 function 中传递参数并将该参数定义为变量?

[英]How can I pass an argument in a function and define that argument as a variable when I call the function?

I have a website where people can make a workshop.我有一个网站,人们可以在那里进行研讨会。

If someone has made a workshop and wants to edit the content they click a button.如果有人创建了一个研讨会并想要编辑内容,他们点击一个按钮。

If that button is clicked the JS checks if there is data in the database for that user and workshop.如果单击该按钮,JS 将检查数据库中是否有该用户和研讨会的数据。 If that is the case the workshop-editor page is opened.如果是这种情况,将打开研讨会编辑器页面。

Depended on how much data is in the database for that workshop certain divs need to be shown (display: block;).取决于该研讨会的数据库中有多少数据,需要显示某些 div(显示:块;)。

In the JS there is a if statement for this.在 JS 中有一个 if 语句。 So:所以:

if(stepOneTitle != "")

When the if statement is true a button is clicked (button.click()) and the div is shown and filled with the data from the database in order for the user to edit the data.当 if 语句为真时,单击按钮 (button.click()) 并显示 div 并填充数据库中的数据,以便用户编辑数据。

So the code for one situation is:所以一种情况的代码是:

if(stepOneTitle != ""){
            const buttonStepOne = document.getElementById("button-step-one")

            buttonStepOne.click()

  }

I don't want to copy and paste this code for all eitgh divs, so I tried to make it into a function which I can call for all eight divs:我不想为所有 eitgh div 复制并粘贴此代码,所以我尝试将其变成 function ,我可以调用所有八个 div:

function loadSteps(a,b,c){
            if(a != ""){
                b = document.getElementById(c)

                c.click()

            };
        };

 loadSteps(stepOneTitle, const buttonStepOne, "button-step-one" )

 loadSteps(stepTwoTitle, const buttonStepTwo, "button-step-two" )

etc.. ETC..

The problem is I can't call the function with 'cont buttonStepOne' because I get a syntax error saying "const" is an unexpected token.问题是我不能用“cont buttonStepOne”调用 function,因为我收到一个语法错误,说“const”是一个意外的标记。

I have tried:我努力了:

function loadSteps(a,b,c){
                if(a != ""){
                  const b = document.getElementById(c)
    
                    c.click()
   
                };
            };

     loadSteps(stepOneTitle, buttonStepOne, "button-step-one" )

The console give me a error saying const b is already declared, so I assume b gets binded to const and not buttonStepOne when I call the function.控制台给我一个错误,说 const b 已经声明,所以我假设当我调用 function 时,b 被绑定到 const 而不是 buttonStepOne。

I've asked on Reddit, but I get workarounds, but no direct solutions.我在 Reddit 上问过,但我得到了解决方法,但没有直接的解决方案。

Is their a direct solution to this issue?他们是这个问题的直接解决方案吗?

You don't pass variables to functions, just values.您不会将变量传递给函数,而只是传递值。 There's no need for the caller to specify the variable name, you can just use a local variable within the function.调用者无需指定变量名,您可以使用 function 中的局部变量。

 function loadSteps(title, buttonId) { if (title.= "") { let button = document.getElementById(buttonId) if (button) { button;click(); } }; }, loadSteps(stepOneTitle, "button-step-one") loadSteps(stepTwoTitle, "button-step-two")

  1. Pass the title and buttonId as the arguments.将 title 和 buttonId 作为 arguments 传递。
  2. Check if the title is present, if present, get the button by using the id.检查标题是否存在,如果存在,则使用 id 获取按钮。
  3. Click on the button.点击按钮。

Also, it's very much appreciated to use descriptive variable names.此外,非常感谢使用描述性变量名称。 Make the code more readable.使代码更具可读性。

function loadSteps(title, buttonId){
                if(title != "") {
                  const button = document.getElementById(buttonId);
    
                    button.click();
   
                };
            };

     loadSteps(stepOneTitle, "button-step-one");

const starts a variable declaration. const开始一个变量声明。 Variable declaration are statements so they cannot be part of the arguments list of a call expression.变量声明是语句,因此它们不能成为调用表达式的 arguments 列表的一部分。

Unclear why you get that error in the second case.不清楚为什么在第二种情况下会出现该错误。 While b is a parameter, that doesn't prevent you from declaring another variable with the same name (especially in a different scope).虽然b是一个参数,但这并不妨碍您声明另一个具有相同名称的变量(尤其是在不同的范围内)。

Either way, you don't need that second parameter.无论哪种方式,您都不需要第二个参数。 It doesn't serve any purpose.它没有任何目的。 You can't declare "dynamic" variables that way and there is no reason to do so anyway.您不能以这种方式声明“动态”变量,无论如何也没有理由这样做。 It doesn't matter what the name of the variable is.变量的名称是什么并不重要。

Just do:做就是了:

function loadSteps(title, id){
  if(title != ""){
    const element =  document.getElementById(id);
    if (!element) {
      console.error(`No such element: ${id}`);
      return;
    }
    element.click();
  }
}

loadSteps(stepOneTitle, "button-step-one" )
loadSteps(stepTwoTitle, "button-step-two" )

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

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