Chrome console.log(elementID)在控制台中输出该元素 [英] Chrome console.log(elementID) outputs the element in the console
问题描述
<input type="text" placeholder="Password" id="password" name="password" />
<script>
console.log(password);
</script>
上面的代码在控制台中输出以下内容:
The code above outputs the following in the console:
<input type="text" placeholder="Password" id="password" name="password">
当我输出变量密码
时注意到这一点并注意到HTML已被预先添加。这是正常的行为吗?在这种情况下我们有什么 getElementById
?
Noticed this when I outputted a variable password
and noticed the HTML was prepended. Is that normal behaviour? What do we have getElementById
for in that case?
推荐答案
获取元素使用窗口[元素ID]
或窗口[元素名称]
是标准行为自Firefox 14以来,href =https://bugzilla.mozilla.org/show_bug.cgi?id=622491 =nofollow noreferrer>。我会推荐你关于这个主题的一些帖子。正如您在大多数帖子中都会发现的那样,不建议使用该行为,并且通常较慢,因为浏览器优化 .getElementById
并检查变量是否为id或name是全球范围内最低优先级。
Getting an element using window[element id]
or window[element name]
is standard behavior implemented by all modern browsers since Firefox 14. I'll refer you to a few posts on the subject. As you'll find in most posts about the matter, use of the behaviour is not recommended and generally slower as browsers optimize .getElementById
and checking if a variable is an id or name is the lowest priority in global scope.
- Do DOM tree elements with ids become global variables?
- Can I use the id of an HTML element as a variable in JavaScript?
这篇关于Chrome console.log(elementID)在控制台中输出该元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!