簡體   English   中英

javascript / jquery修改一個css類

[英]javascript/jquery modify a css class

在加載我的頁面之前,我想對類樣式進行一些修改。 樣式類包含在MyStyle.css

.divNew {
    float: left; 
    margin: 10px 5px 5px 25px;
}

並加載一個新的頁面,我想修改/重載邊距和增加高度。 有什么建議嗎? 這就是我嘗試過的:

function pageLoad() {
    var theRules = new Array();
    if (document.styleSheets[0].cssRules) {
        theRules = document.styleSheets[0].cssRules;
    } else if (document.styleSheets[0].rules) {
        theRules = document.styleSheets[0].rules;
    }
    for (n in theRules) {
        if (theRules[n].selectorText == 'divNew') { ? ? ? ?
        }
    }
}

在Document ready上使用jQuery,你可以做這樣的事情。

$(document).ready(function(){
    $('.divNew').css('margin','modifications');  // .css('margin','10px 10px 10px 10px');
    $('.divNew').height(newHeight);  // .height(300)
});

以供參考

http://api.jquery.com/css/

使用jquery css函數。 它需要一個CSS屬性的對象。

$(document).ready(function(){
    $('.divNew').css({
        "margin": "10px 5px 5px 25px",
        "height": "100px" //change to whatever your height is
    });
});

你將這個問題復雜化了10億。

$('.divNew').css({'height':'2000px', 'width':'2000px'});

只需使用內置的jquery css方法使用逗號分隔值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM