gpt4 book ai didi

javascript - Node.getAttribute 的运算符计算不一致

转载 作者:行者123 更新时间:2023-12-03 04:27:52 28 4
gpt4 key购买 nike

我似乎偶然发现了一个看似错误的等价物,使用以下命令生成维基百科主页 (wikipedia.org) 的所有输入的列表:

var inputs = Array.prototype.slice.call(document.getElementsByTagName("input"));

给我一​​个所有输入的列表,但是,<input#searchInput>具有 type=search 的节点属性的评估似乎非常奇怪。

inputs[2].getAttribute('type') === ("text" || "search")计算结果为 false

inputs[2].getAttribute('type') === ("search" || "text")计算结果为 true .

谁能解释一下这里面的逻辑吗?

最佳答案

术语 (string || string) 似乎计算为第一个非 null 值,或者如果两者都为 null,则计算为 null,这是有道理的。

因此 ("text"|| "search") 结果为 "text"。在该逻辑中,它评估括号中的比较,然后执行:inputs[2].getAttribute('type') === "text",即 false

为了证明这一点,请尝试以下应该返回 true 的代码:

inputs[2].getAttribute('type') === (null || "search")

关于javascript - Node.getAttribute 的运算符计算不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43621076/

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