如何根据标记的ID设置类的css left属性

时间:2015-10-29 04:57:44

标签: jquery css

我实际上是在基于li标签id设置类的左属性。我正在动态生成li标签,如下所示:

的.js

var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
$.each(months, function (i, v) {
    var li = $('<li id="sp_month_' + v + '">' + v + '</li>')
    $('#servicePlanMonths').append(li);
});

我将LI标签ID作为 sp_month_jan 等等......

现在使用这些Id我想将另一个div的.css left属性(这也是如下动态生成的)设置为与每个li id完全相同。下面是生成另一个div的代码:

的.js

function DrawSPPriorityTimeLine(curelement, priorityObj) 
{
var priorityMonth = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var timeFrame = priorityObj.TimeFrames();
var activityTotalMonth = [];
//$this.activityTotalMonth = ko.observableArray([]);

if (timeFrame.trim().length > 0 && timeFrame.trim() != "On-going") {
    var startMonth = jQuery.inArray(timeFrame.split('-')[0].trim(), priorityMonth);
    var endMonth = (timeFrame.split('-').length > 1) ? jQuery.inArray(timeFrame.split('-')[1].trim(), priorityMonth) : startMonth;
} else {
    var startMonth = 0;
    var endMonth = 11;
}

//var temp1 = priorityMonth.slice(startMonth, priorityMonth.length);
//var temp2 = priorityMonth.slice(endMonth, priorityMonth.length);
//priorityMonth = $.merge(temp1, temp2);

var activityMonth = (endMonth - startMonth + 1) * 3 - 2;

for (var j = 0; j <= activityMonth; j++)
    activityTotalMonth.push(j);

$.each(activityTotalMonth, function (i, v) {
    var div = $('<div>').addClass('counts');        
    $(curelement).find('.bar-steps').append(div);
});

}

的CSS

.bar-steps .counts {
    width: 3px;
    height: 3px;
    float: left;
    background: #999;
    margin-right: 22px;
    border-radius: 50px;
}

div是动态生成的,我附加了“count”类,以便它也渲染样式。

问题在于,当生成这些div(附加类计数)时,div不与li标记对齐。

在Li标签的帮助下,我将水平显示的月份名称完全正常工作。现在我正在尝试对齐另一个div的left属性(使用类“counts”),它将开始从显示li文本(例如月份名称Feb)的相同位置生成div。

2 个答案:

答案 0 :(得分:0)

您可以尝试以下选项:

  1. document.getElementById("myBtn").style.left = "100px";
  2. document.getElementById("myDiv").style.marginLeft = "50px";

答案 1 :(得分:0)

我进一步分析并得到了解决方案。以下是我之前期待的结果:

// This variable will contain the left position value for the starting month in the priority's time frame 
var monthLeftPosition = $('#servicePlanMonths').find('#sp_month_' + startingMonth).position().left;

$(curelement).find('.bar-steps').offset({ left: monthLeftPosition });