如果在给定的示例中调用了 JavaScript 函数,则 JavaScript 函数采用哪个值

which value does javascript function takes if the function is called in the given example?

本文关键字:JavaScript 函数 调用 如果      更新时间:2023-09-26

我是javascript新手,并试图只用纯javascript来制作todo

我有一个输入字段。

input(type="text", placeholder="Type here", id="new-todo", name="newTodo")

当我调用function创建一个新todo时,首先function会检查输入id或输入name

假设我有一个池塘,它只有两种鱼当我在那个池塘里拿着鱼竿去钓鱼时,鱼竿一开始会钓到什么样的鱼。

在上面,我将池塘与input进行了比较,两种鱼idname和钓鱼棒与function,我将调用它来创建新todo.

任何人都可以解释我理解这个问题吗...

通过使用与pondfishesfishing stick相同的类比。

池塘是一个form。鱼是它的elements(不是属性 id 或名称)。每条鱼/元素都有自己的nameid。现在,您可以通过其属性ID或名称捕获鱼。

在 JavaScript 中:

document.getElementById("firstFishId")document.forms[0].firstFishName 将为您提供相同的元素,如果您的 html 中有以下标记。

<form> <!-- This is a pond -->
<input type="text" name="firstFishName" id="firstFishId" /> <!-- this is a fish with id `firstFishId` and a name `firstFishName` -->
<input type="text" name="secondFishName" id="secondFishId" /> <!-- this is a fish with id `secondFishId` and a name `secondFishName` -->
</form>

希望它能帮助你理解一点。