无法在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 ?)
我应该使用其他东西吗?
Should I use something else ?
如果是这样:
什么?
为什么 .style.something
不起作用?
If so :
What ?
Why .style.something
doesn't work ?
推荐答案
p>根据 文档 :
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的/ jqLite的 .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屋!