我希望能够获取元素上动态设置的所有样式(即应用为内联样式)。
例如:
<span class="text" style="color: rgb(128, 128, 255); font-size: 24px;">Name</span>
我想将style属性的值保存在JS变量中,并获取它。我尝试使用jQuery的.attr('style'),但它返回undefined。
同时,根据这里的建议(How to get inline CSS style property from element),可以使用...
getComputedStyle
但是为了获得样式,我需要提及所有的样式,例如:
var el = document.querySelector("selector")
console.log(el.style.SomeStyle);
但是用户可以动态设置各种样式,那么我需要逐个提及所有的内联样式吗?还是有更好的方法可以实现呢?
非常感谢您的帮助。
更新自void的评论:
正如在这里描述的那样,marknadal编写了一个函数来检索内联和外部样式,但我只需要与所有附加CSS类无关的内联样式。