[英]How Can I set variable width with !importanat using JavaScript/jQuery
我有一個名為“#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
使用 ES6,
var width = "100px";
$(".event").attr('style', `width: ${width} !important`);
像這樣添加:
$('.event').attr('style', 'width: '+ setWdt +' !important');
您可以使用 jquery 的 css 屬性,請查看以下代碼片段:
$(".event").css( "width", function(
setWtd ) {
return setWtd + '!Important';
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.