如何使用JavaScript / jQuery使用!importanat设置可变宽度

时间:2019-11-30 07:25:44

标签: javascript jquery bootstrap-4

Ia有一个名为“ #idDayNrCell”的div。我想获取其宽度,然后进行一些计算,并将其应用于另一个名为“ .event”的div。我正在使用引导程序,因此我也需要应用!important。我是javascript / jquery的新手。

我尝试过这样的事情。但这没问题

$(document).ready(function(){

    var cellWDTstr = ($("#idDayNrCell").css("width")); //get width
    var cellWDT =  cellWDTstr.substr(0,cellWDTstr.length-2); //remove the "px" part
    console.log(cellWDT);

    var GunSayisi=2; //how long is the event (2 for example)

    // after tihs things get complicated for me
    // if the even is minimum 2 days i need a different width. calculated below
    var wdtEnAz2 = ((cellWDT-30)*GunSayisi + 30*(GunSayisi-1)).toString(); 
    console.log(wdtEnAz2);
    var setWdt = GunSayisi>1 ? wdtEnAz2 : calWdt;

    //after here it is expoerimental code which I am failed
    console.log(setWdt);
    setWdt+= 'px';
    console.log(setWdt);
    $(".event").style.setPsetProperty('width',setWdt,'important');


});

这是html

3 个答案:

答案 0 :(得分:2)

使用ES6

var width = "100px";

$(".event").attr('style', `width: ${width} !important`);

答案 1 :(得分:1)

像这样添加:

         $('.event').attr('style', 'width: '+ setWdt +'  !important');

答案 2 :(得分:0)

您可以在jquery中使用css属性,请在下面的代码段中找到该:

$(".event").css( "width", function(  
setWtd ) {

return setWtd + '!Important';

});