角和角的区别是什么?element和document.getElementById
what is the difference between angular.element and document.getElementById
Angularjs
get element by id:您可以使用angular.element(‘#element_id’);
通过id获取元素,其中#element_id
是元素的id。
这是javascript document.getElementById
在angularjs中的替代-
Angularjs获取元素id:
var currentElement = angular.element('#element_id');
和有什么区别呢?element and document.getElementById
我会添加到@evolutionxbox答案,document.getElementById
应该可能比angular.element
稍快,因为它不需要通过计算传递到函数的例行程序以及应该基于此采取的步骤。
关于什么时候应该使用。因为这两种方法返回不同的对象类型(jquery vs HTMLElement),你可以根据应用程序中的标准实践来选择方法——是使用jquery元素还是使用原生dom元素进行操作,或者根据你要对它们执行的操作来选择方法,因为使用jquery对象比使用原生HTMLElement更容易完成一些操作。
例如,如果您想使用任何jquery方法,如.hasClass()
,则需要像angular.element(document.getElementById('some')).hasClass()
这样包装元素,因此首先使用angular.element('#some').hasClass();
更容易。
一般来说,我会选择angular.element
方法,因为它是angular原生指令中的首选方法。
当你选择一个ID为angular. js的对象时。元素(或jQuery),它采用快捷方式并使用document.getElementById
.
最大的区别是angular.element
是一个jQuery别名,并返回一个jQuery(或jQuery lite)对象。
而document.getElementById
是一个本地DOM方法,它返回一个HTMLElement对象。
tl:博士;
始终阅读文档
- 正在寻找比$(document).ready慢的$(window).load的替代方案
- document.open/document.write没有正确地清除chrome中的文档——这是chrome的错误吗
- 如何用更合适的内容替换document.write
- document.styleSheets不返回任何内容
- jQuery document.ready not working
- $(document).height()在刷新时随机化值(Safari 5.1.10)
- reactjs this.refs vs document.getElementById
- 如何通过adf中的document.getElementById获取inputText字段值
- window.opener.document在ie中不起作用
- contentWindow.document.body is null
- document.applet.method在Mounatin Lion上抛出safari 6+JDK7异常
- document.getElementById(“st”).click();不起作用
- $(document).height和$(window).heaght都返回相同的值-使用的是正确的doctype
- jQuery document.ready停止代码
- document.getElementById并使用id名称
- 使用jQuery 1.8.1准备好多个$(document).是否有任何开销
- document.getElementById在js中不起作用
- document.getElementById.style.backgroundImage not working
- 停止对document.ready函数的重定向/刷新
- 是否可以 document.getElementsByTagName('head')[0] ever 返回 null