javascript AngularJS 在没有 JQuery 的情况下获取元素属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18852687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AngularJS Get Element Attributes without JQuery
提问by Devin Dixon
I am working on creating a AngularJS site that DOES NOT use JQuery. In a directive, the values that are passed are
我正在创建一个不使用 JQuery 的 AngularJS 站点。在指令中,传递的值是
EngagementApp.directive('movingAside', function() {
return {
restrict : 'A',
link : function(scope, element, attributes) {
}
}
});
And html looks like:
和 html 看起来像:
<div class="aside" moving-aside>...Content...</div>
Doing things like element.clientHeight returns an undefined.
做 element.clientHeight 之类的事情会返回一个未定义的。
How can get the attributes(height, width, offset, etc) of the element without using JQuery and only AngularJS functions?
如何在不使用 JQuery 和仅使用 AngularJS 函数的情况下获取元素的属性(高度、宽度、偏移量等)?
回答by Marko
Use element[0].clientHeight | offsetHeight | scrollHeight
利用 element[0].clientHeight | offsetHeight | scrollHeight
element[0]
gives you access to the first DOM element in the JQLite selector collection while clientHeight
is a built in property of the DOM element.
element[0]
允许您访问 JQLite 选择器集合中的第一个 DOM 元素,而clientHeight
是 DOM 元素的内置属性。
EngagementApp.directive('movingAside', function() {
return {
restrict : 'A',
link : function(scope, element, attributes) {
console.log(element[0].clientHeight);
}
}
});
There are 3 different heights you can get depending on what you're after:
根据您的追求,您可以获得 3 种不同的高度:
回答by massintha
Try angular.element(element).css('height')
尝试 angular.element(element).css('height')