Chrome console.log(elementID)在控制台中输出该元素 [英] Chrome console.log(elementID) outputs the element in the console

查看:1041
本文介绍了Chrome console.log(elementID)在控制台中输出该元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆