且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用jquery更改页面的内联样式

更新时间:2022-10-23 09:50:23

尝试 jQuery.Rule



然后你会这样做:

  $。rule('body {backgound-color:#ff0000}')appendTo('style'); 

将身体的背景颜色更改为眼睛红色。



如果你只是想添加或覆盖特定的东西,你可以使用普通的jQuery添加一个 style 属性到基于id / class的特定元素:

  //添加到id =foo的元素
$('#foo')。attr style,{backgroundColor:#ff0000})
//使用class =bar添加到所有元素
$('。bar')。attr #ff0000})


I have an inline styles in my page as :

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>:: Inline Style Page ::</title>
<style type="text/css">
    <!--
    body
    {
        background: #fff;
        font: normal 12px Tahoma, Verdana, Arial;
        color: #636363;
    }
    #header
    {
        height: 60px;
    }
    #menu
    {
        background: #fff;
        height: 30px;
    }
    #footer
    {
        height: 50px;
        background: #fff;
    }
    -->
</style>

I need to update or add some styles to the inline styles using jquery..

I am trying to update the font, background color of the page/divs/controls using jquery. I will have my inline styles (loaded from database) for the style of my page content. My application's user can change some of the styles and update it back to the database.

Its almost like creating custom page application(asp.net mvc in c#).

How can i change the inline styles of the page using jquery? or is there any other effective way to do it?

Try jQuery.Rule

Then you would do:

$.rule('body { backgound-color: #ff0000 }').appendTo('style');

to change the background-color of the body to eye burning red.

If you just want to add or override specific things you can use regular jQuery to add a style attribute to specific elements based on id/class:

// add to element with id="foo"
$('#foo').attr("style", {backgroundColor: "#ff0000"}) 
// add to ALL elements with class="bar"
$('.bar').attr("style", {backgroundColor: "#ff0000"})