Javascript:在没有getElementById的情况下访问DOM元素 [英] Javascript: access DOM elements without getElementById

查看:75
本文介绍了Javascript:在没有getElementById的情况下访问DOM元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一些示例代码。

Below is some sample code.

<body>
    <div id="wrapper" class="access">
        <form id="test">
            <input id="password"></input>
            <input type="submit"></input>
        </form>  
    </div>
    <script>
        console.log(password);
        alert(wrapper.className);
    </script>
</body>

控制台日志返回ID为 password 。警报为警报。

The console log returns the DOM element with id password. The alert is "alert".

它也可以正常代码运行,例如 var x = wrapper.className;

It works in normal code as well, e.g. var x = wrapper.className;

当然这不对吗? document.getElementById $('#password') goog的意义是什么.dom.getElement 是否可以像这样工作?这些功能现在仅适用于旧版吗?

Surely this is not right? What is the point of document.getElementById, $('#password') or goog.dom.getElement if it just works like this? Are these functions just for legacy now?

顺便说一句,它可在Firefox,Chrome和Safari中使用。

It works in Firefox, Chrome and Safari, incidentally.

推荐答案

这个答案应该给您带来很多帮助:

This answer should give you a lot to go on:

https:/ /stackoverflow.com/a/6381766/944006

您不应该使用这些变量,并且您绝对不希望依赖于它们的正常工作跨浏览器。

You shouldn't use these variables, and you DEFINITELY don't want to depend on them working correctly across browsers.

这篇关于Javascript:在没有getElementById的情况下访问DOM元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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