javascript/jquery修改一个css类

标签 javascript jquery html css

在加载我的页面之前,我想对类样式进行一些修改。 样式类包含在 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') { ? ? ? ?
        }
    }
}

最佳答案

使用 jQuery on Document ready 你可以做这样的事情。

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

供引用

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

关于javascript/jquery修改一个css类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19771598/

相关文章:

jQuery - 异步发送表单

jquery - 将多个复选框值保存到 sql 数据库

html - 页脚下方的额外空间

javascript - firefox 和 radialgradient(使用 html5 Canvas )

javascript - 谷歌地图 : How to create a custom InfoWindow?

javascript - 将 HTML5 PhoneGap App 连接到在线数据库

javascript - 网页设计 : Avoiding a horizontal "jump" of the content by adding a margin-left. 但这是如何工作的呢?

php - gzip 不适用于内部 css 和 javascript

javascript - Owl Carousel 选项未被读取

javascript - Woocommerce:未触发交付方法更改事件处理程序