无法在 AngularJS 中更改 CSS 样式 [英] Can't change CSS style in AngularJS
问题描述
由于某些原因,我无法使用以下内容更改元素的样式:
For some reasons, I can't change the style of an element using the following :
angular.element("#element").style.height = 100px;
我确信 angular.element("#element")
可以工作,因为它返回一个 DOM 元素.另外,这有效:
I'm sure that angular.element("#element")
works, as it returns a DOM element. Plus, this works :
angular.element("#element").addClass('something');
(我发现的一切都是关于 ngStyle 的,但我认为这不是我要找的?)
(Everything I've found is about ngStyle but I don't think it is what I'm looking for ?)
我应该用别的东西吗?
如果是这样:什么 ?为什么 .style.something
不起作用?
If so :
What ?
Why .style.something
doesn't work ?
推荐答案
根据文档:
According to the docs:
注意:Angular 中的所有元素引用总是用 jQuery 或 jqLite 包裹;它们从来都不是原始 DOM 引用.
所以 .style
属性不能直接使用,因为它是包装好的 HTMLElement
.
So the .style
property is not available directly, since it is a property of the wrapped HTMLElement
.
您可以使用 ivarni 的方法来获取 HTMLElement (angular.element(...)[0].style...
) 或使用 jQuery's/jqLite's .css()
方法:
You can either use ivarni's approach to get hold of the HTMLElement (angular.element(...)[0].style...
) or use jQuery's/jqLite's .css()
method:
angular.element('#element').css('height', '100px');
这篇关于无法在 AngularJS 中更改 CSS 样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!