简体   繁体   English

您可以从事件侦听器一次触发两个或多个函数吗?

[英]Can you fire off two or more functions at once from an event listener?

https://jsfiddle.net/Jbautista1056/pvdq6gye/18/ https://jsfiddle.net/Jbautista1056/pvdq6gye/18/

I need help getting my event listener to fire on a specific function, but it doesn't seem to be changing anything.我需要帮助让我的事件侦听器在特定的 function 上触发,但它似乎没有改变任何东西。 I'm working on the etch-a-sketch project and I'm trying to update the grid-template-columns/rows based on the user input size.我正在研究 etch-a-sketch 项目,我正在尝试根据用户输入大小更新网格模板列/行。 I'm able to create the div's onclick but the div's don't adhere to the grid-template-columns/rows format I'm trying to do.我能够创建 div 的onclick但 div 不遵守我正在尝试做的网格模板列/行格式。 For example, if someone typed 4, it would show a total of 4 rows and 4 columns with the elements following that format in that container div.例如,如果有人键入 4,它将在该容器 div 中显示总共 4 行和 4 列,其中元素遵循该格式。 Right now it only creates 1 column and 1 row no matter what I input, because in my CSS file the default is repeat(1, 1fr) for both grid rows/columns.现在,无论我输入什么,它都只会创建 1 列和 1 行,因为在我的 CSS 文件中,两个网格行/列的默认值为repeat(1, 1fr) I added the createGrid() and columnsAndRows() functions to the event listener onclick in the html file, but only the createGrid() function actually fires.我在 html 文件中的事件监听onclick中添加了 createGrid( createGrid()columnsAndRows()函数,但实际上只有 createGrid createGrid() function 会触发。

This is the code I suspect I might be doing wrong but can't think of any other alternatives:这是我怀疑我可能做错但想不出任何其他选择的代码:



function columnsAndRows() {
    gridContainer.style.gridTemplateColumns = "repeat(sizeValue, 1fr)"
    gridContainer.style.gridTemplateRows = "repeat(sizeValue, 1fr)"
  }

function createGrid() {
    let parent = document.getElementById("tile")
    for (let i = 0; i < gridSize -1; i++) {. //gridSize is the total amount of squares need to be made
        let baby = document.createElement("div");
        baby.className = "babytiles"
        parent.before(baby)
    }
} ```

```html 
  <form id="totalGridSizeForm" > 
        <input type="number" placeholder="size" min="1" max="64" id ="size" value="" oninput="getSizeValue()">
        <input type="button" id="Submit"  value="Submit" onclick="createGrid()" onclick="columnsAndRows()" >
      </form>

In HTML, an element can't have two of the same attributes, so your second onclick attribute is being ignored as demonstrated below:在 HTML 中,一个元素不能有两个相同的属性,因此您的第二个onclick属性将被忽略,如下所示:

 <div title="Message 1" title="Message 2">Hover over me</div>

While you could change your onclick attribute value to be:虽然您可以将onclick属性值更改为:

<input type="button" id="Submit"  value="Submit" 
       onclick="function() { createGrid(); columnsAndRows(); }">

or, you could create a third function that simply calls the other two and then use that third function as the onclick function to call, inline HTML event attributes should not be used . or, you could create a third function that simply calls the other two and then use that third function as the onclick function to call, inline HTML event attributes should not be used . Instead, set them up in JavaScript using .addEventListener() , like this:相反,使用 .addEventListener .addEventListener()在 JavaScript 中设置它们,如下所示:

 document.getElementById("Submit").addEventListener("click", function(){ function1(); function2(); }); function function1(){ console.log("function1 fired;"). } function function2(){ console;log("function2 fired!"); }
 <input type="button" id="Submit" value="Submit">

Also (FYI), since you aren't actually using your input fields to submit data anywhere, you shouldn't be using the form tag or a submit button.另外(仅供参考),由于您实际上并没有使用输入字段在任何地方提交数据,因此您不应该使用form标签或submit按钮。 Instead, just use a regular button:相反,只需使用常规按钮:

<button type="button">Submit</button>

or或者

<input type="button" value="Submit">

You could also create a handler function that calls both methods.您还可以创建调用这两种方法的处理程序 function。

Not sure if it matters for your code, but you'll probably want to make it an async method as well depending on which one you want to run first.不确定它是否对您的代码很重要,但您可能还希望将其设为异步方法,具体取决于您要首先运行的方法。

something like this像这样的东西

async function handleOnClick() {
  await createGrid();
  await columnsAndRows();
}

Just wrap both of your functions inside one function.只需将您的两个函数都包装在一个 function 中。 Then call that function.然后调用 function。

 <script>
   function main() {
        function firstFunction() {

        }
        function secondFunction() {

        }
    }
</script>
<input type="button" id="Submit"  value="Submit" onclick="main()" />

or或者

<script>
   function firstFunction() {

   }
   function secondFunction() {

   }
   function main() {
        firstFunction()
        secondFunction()
    }
</script>
<input type="button" id="Submit"  value="Submit" onclick="main()" />

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

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