如何使用jQuery以内联样式获取css
我正在从asp.net mvc(C#)应用程序中的数据库中动态加载样式.
I am loading the styles dynamically from the database in my asp.net mvc (C#) application.
我正在尝试更改已加载的内联样式的某些属性,例如(背景,字体颜色,字体大小等).我正在使用jquery.rule来做到这一点.
I am trying to change some of the properties like (background, font color, font size,...) of the loaded inline style. I am using jquery.rule to do this.
我需要使用jquery将包括更改在内的完整内联样式更新回数据库.
I need to update the complete inline style including the changes, back to the database using jquery.
头部的内联样式如下:
<style type="text/css">
<!
-- body
{
background: #fff;
margin: 0px;
padding: 0px;
font: normal 12px Tahoma, Verdana, Arial;
color: #636363;
}
a
{
color: #d0d0d0;
text-decoration: none;
}
#header
{
padding-left: 35px;
height: 60px;
vertical-align: middle;
padding-top: 25px;
}
-- ></style>
我需要获取更新的内联样式.怎么做?
I need to get updated inline style. How to do it?
我需要使用jquery将包括更改在内的完整内联样式更新回数据库.
I need to update the complete inline style including the changes, back to the database using jquery.
您是否要读取页面中元素的内联样式声明?如果是这样,这很棘手.从理论上讲,您应该能够调用element.getAttribute('style')
或等效的jQuery.但是,DOM属性访问在IE中不起作用.实际上,IE根本不存储文档中使用的属性,而仅存储由此产生的已解析样式声明.
Are you trying to read the inline style declarations of an element in the page? If so, this is tricky. In theory you should be able to call element.getAttribute('style')
or the jQuery equivalent. However DOM attribute access doesn't work in IE; in fact IE doesn't store the attribute as used in the document at all, only the parsed style declarations that result from it.
There's not a jQuery-specific means of reading all styles, but you can get the effective inline style rules as CSS using DOM Level 2 Style.
var style= element.style.cssText;
但是在IE中,这将分隔您已使用的所有快捷方式属性,例如设置border
可能会导致您返回border-style
,border-color
和border-width
. IE还将属性名称大写.这可能对您无关紧要.
But in IE this will separate any shortcut properties you have used, for example setting border
can result in you getting border-style
, border-color
and border-width
back. IE will also upper-case the property names. This may or may not matter to you.
最好记住在单独的查找对象中所做的内联样式更改,这样您可以更轻松地阅读它.您可以使用jQuery的data()
方法将其附加到元素上,对真实" element.style
和查找$(element).data('stylestore')
对象进行所有更改,然后在要发布时从查找中检索所有设置的样式.
You'd probably be better off remembering the inline style changes you make in a separate lookup object so you can read it more easily. You can attach that to the element using jQuery's data()
method, make all changes to both the ‘real’ element.style
and the lookup $(element).data('stylestore')
objects, then retrieve all set styles from the lookup when you're about to post.