Javascript 如何通过jQuery将CSS添加到类中?

Javascript 如何通过jQuery将CSS添加到类中?,javascript,jquery,css,Javascript,Jquery,Css,我正在使用jQuery Mobile开发一个简单的移动应用程序,我想在其中添加一个CSS。目前,我为该类提供了以下CSS .home-button-edited { border: 1px solid #2373A5; margin-top: 1px !important; background:#3496da; } 在JS部分中,我有以下代码段: var viewport = { width: $(window).width(), height: $(

我正在使用jQuery Mobile开发一个简单的移动应用程序,我想在其中添加一个CSS。目前,我为该类提供了以下CSS

.home-button-edited {
    border: 1px solid #2373A5;
    margin-top: 1px !important;
    background:#3496da;
}
在JS部分中,我有以下代码段:

var viewport = {
    width: $(window).width(),
    height: $(window).height()
};

document.addEventListener("deviceready", setHomeButton, false);

function setHomeButton() {
    var difference = viewport.width - 85;
    $('.home-button-edited').css("left", "difference !important");
}
关键是,我想向.home按钮添加一个left属性,该属性经过编辑,其值包含在差分变量中,并且它还应该有一个!重要财产。有什么想法吗

 $('.home-button-edited').css("left", difference+"!important");


函数中的错误

function setHomeButton() {
    var difference = viewport.width - 85;
    $('.home-button-edited').css("left", difference + " !important");
}

您可以在您的案例中使用:

$('.home-button-edited').css('cssText','left: '+difference+'px !important');

尝试
$('.home button edited').css(“左”,差异),按照您的方式,您将
差异设置为字符串,而不使用变量。请与我共享您代码的链接!重要的是不好的,最好的解决方案是在CSS中去掉它来开始。@epascarello但是我的代码确实需要它。无论如何
!重要信息
语句在使用jQuery
css()时不起作用
method@A.Wolff是的,它似乎完全忽略了这一点!重要部分。你知道如何在JS或jQuery中实现我想要的吗?试试这样的方法(只是从其他地方截取了一个片段)$('.myclass').each(函数(){this.style.setProperty('height','0px','important');});@ett Use-Use-attribute style或
.css('cssText','left:'+difference+'px!important'))
或更好,在任何情况下都不要使用
!重要的
语句rules@A.Wolff“cssText”部分到底是什么?
!重要的
语句在使用jQuery css()时不起作用method@MayankTripathi是的,A。沃尔夫是对的,它似乎不起作用。是的。我的错误。它不起作用。抱歉!
$('.home-button-edited').css('cssText','left: '+difference+'px !important');