我怎样才能获得使用AngularJS元素的宽度是多少? [英] How can I get the width of an element using AngularJS?
本文介绍了我怎样才能获得使用AngularJS元素的宽度是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道我可以使用jQuery获取元素的宽度,但我很好奇;有没有办法这样做使用AngularJS?
I realise I can use jQuery to get the width of an element, but I'm curious; is there a way to do so using AngularJS?
推荐答案
本机JS(#ID):
document.getElementById(id).clientWidth;
本机JS(querySelector(类,ID,CSS3选择)):
Native JS (querySelector (class, id, css3 selector)):
document.querySelectorAll(".class")[0].clientWidth;
代入angular.element这样的:
Plugging this into angular.element:
angular.element(document.getElementById(id)).clientWidth;
angular.element(document.querySelectorAll(".class")[0]).clientWidth;
如果您需要得到非圆形的号码,使用getBoundingClientRect。
If you need to get non rounded numbers, use getBoundingClientRect.
angular.element(document.getElementById(id)).getBoundingClientRect();
关于这个问题的一些资源:
Some resources on the subject:
- http://msdn.microsoft.com/en-us/library/hh781509(VS.85).aspx
- https://developer.mozilla.org/en-US/docs/Web/API/Element.clientWidth
- https://developer.mozilla.org/en-US/docs/Web/API/element.getBoundingClientRect
如果这个漂浮在语法方面你的船和无必要的jQuery插件到组合,谨防跨浏览器的地狱。
If this floats your boat in terms of syntax and the no-need to plug jQuery into the mix, be wary of cross browser hell.
这篇关于我怎样才能获得使用AngularJS元素的宽度是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文