简体   繁体   English

CSS高度属性被for循环中最后计算出的height参数覆盖

[英]CSS height property is getting overwritten by last calculated height parameter in a for loop

I have written a JQuery snippet that iterates through a JSON array and calculates a "new height" parameter which has to be assigned to the CSS property for the list item being appended. 我编写了一个JQuery代码段,该代码遍历JSON数组并计算了一个“新高度”参数,该参数必须分配给要附加的列表项的CSS属性。 The problem here is that the last calculated height is getting assigned for all the list items. 这里的问题是为所有列表项分配了最后计算的高度。 ie if groups[]={2,3} , the height is correctly calculated as 52 and 78. But the css being assigned is having a value of 78 for both the list items. 例如,如果groups [] = {2,3},则正确计算出高度为52和78。但是,两个列表项所分配的css的值为78。

Below is the code snippet: 下面是代码片段:

for(k=0; k<JSONObj.rackData[rackIdToBeDisplayed].groups.length; k++){                   
 newHeight = JSONObj.rackData[rackIdToBeDisplayed].groups[k] * 26;                  
 $('#rackBoxLi .list').append('<li id="listElement">Device Added</li>');
 $('#rackBoxLi .list #listElement').css({"height": newHeight + 'px'});                  
 }

CSS for listElement is as follows - listElement的CSS如下-

#listElement{
background-color: #0099CC;
border:1px dashed black;
border-radius: 5px;
width:180px;
margin-bottom: 5px;
}

Just put the style directly on the element before you add it; 只需将样式直接添加到元素上即可; it'll be faster anyway: 无论如何它会更快:

for(k=0; k<JSONObj.rackData[rackIdToBeDisplayed].groups.length; k++){                   
     newHeight = JSONObj.rackData[rackIdToBeDisplayed].groups[k] * 26;                  
     $('#rackBoxLi .list').append($("<li/>", {
       text: "Device Added",
       css: { height: newHeight + "px" }
     }));          
}

That way you don't have to do the DOM lookup at all. 这样,您根本不需要进行DOM查找。

And as has been chanted in unison, don't put the same id on every element you add. 并一如既往地高呼,不要在添加的每个元素上都放置相同的ID。

It looks like your ID specified in the JavaScript is the same every time: 看起来您在JavaScript中指定的ID每次都是相同的:

$('#rackBoxLi .list').append('<li id="listElement">Device Added</li>');

If they all have the same ID, then this selector will match all of them every time: 如果他们都具有相同的ID,那么这个选择器将匹配所有 每次他们:

$('#rackBoxLi .list #listElement').css({"height": newHeight + 'px'});

The point of an ID is that it is unique per element - so that you can select an ID and know you're only selecting one element. ID的要点是每个元素都是唯一的-因此您可以选择一个ID并知道只选择一个元素。 To fix this you could change it so that it's using your iterator ( k ) in order to make them all different: 要解决此问题,您可以对其进行更改,以使其使用迭代器( k )来使它们全部不同:

for(k=0; k<JSONObj.rackData[rackIdToBeDisplayed].groups.length; k++){                   
 newHeight = JSONObj.rackData[rackIdToBeDisplayed].groups[k] * 26;                  

 //Notice that it's inserting k into the id, so that they'll all be different
 $('#rackBoxLi .list').append('<li id="listElement' + k + '">Device Added</li>');

 $('#rackBoxLi .list #listElement' + k).css({"height": newHeight + 'px'});                  
}

In order to still be able to give them all the same style in your CSS file, you should give them the same class , like this: 为了仍然能够在CSS文件中为它们提供所有相同的样式,应为他们提供相同的 ,如下所示:

for(k=0; k<JSONObj.rackData[rackIdToBeDisplayed].groups.length; k++){                   
 newHeight = JSONObj.rackData[rackIdToBeDisplayed].groups[k] * 26;                  

 //This way they'll all have the same class, but different IDs
 $('#rackBoxLi .list').append('<li class="listElement" id="listElement' + k + '">Device Added</li>');

 $('#rackBoxLi .list #listElement' + k).css({"height": newHeight + 'px'});                  
}

And then change your CSS to reference it by class instead of ID. 然后更改您的CSS以按类而不是ID引用它。

.listElement{
background-color: #0099CC;
border:1px dashed black;
border-radius: 5px;
width:180px;
margin-bottom: 5px;
}

You shouldn't use the same id for multiple elements. 您不应对多个元素使用相同的ID。 No need to re-select the created item either. 也无需重新选择创建的项目。 You can just do it like this: 您可以这样做:

for(k=0; k<JSONObj.rackData[rackIdToBeDisplayed].groups.length; k++){                   
 newHeight = JSONObj.rackData[rackIdToBeDisplayed].groups[k] * 26;                  
 $('<li>Device Added</li>').css({ "height" : newHeight + 'px'}).appendTo('#rackBoxLi .list');             
 }

The first thing I see here is that you're adding multiple items to your list, and assigning them all the same ID. 我在这里看到的第一件事是,您要将多个项目添加到列表中,并为它们分配相同的ID。 IDs should only be used once, try a class instead. ID只能使用一次,请尝试一个类。

As to your question, when you use a selector in JQuery it grabs all elements on the page that fit what you have specified. 关于您的问题,当您在JQuery中使用选择器时,它将获取页面上所有适合您所指定内容的元素。 So every time you say "#rackBoxLi .list #listElement" it grabs anything it finds that matches that, which would be all of the items added thus far. 因此,每次您说“ #rackBoxLi .list #listElement”时,它都会捕获与之匹配的所有内容,这就是到目前为止添加的所有项目。 Try "#rackBoxLi .list #listElement:last", which would just highlight the last element in the group, which would be the latest added item. 尝试“ #rackBoxLi .list #listElement:last”,它将仅突出显示组中的最后一个元素,这是最新添加的项目。

In your loop you are setting the CSS which is applied to all the elements that match #rackBoxLi .list #listElement 在循环中,您将设置CSS,该CSS将应用于与#rackBoxLi .list #listElement匹配的所有元素

for(k=0; k<JSONObj.rackData[rackIdToBeDisplayed].groups.length; k++){                   
    newHeight = JSONObj.rackData[rackIdToBeDisplayed].groups[k] * 26;                  
    $('#rackBoxLi .list').append('<li id="listElement">Device Added</li>');
    $('#rackBoxLi .list #listElement').css({"height": newHeight + 'px'});                  
}

IDs are supposed to be unique to each element so try this instead. ID对于每个元素都是唯一的,因此请尝试使用此方法。

for(k=0; k<JSONObj.rackData[rackIdToBeDisplayed].groups.length; k++){                   
    newHeight = JSONObj.rackData[rackIdToBeDisplayed].groups[k] * 26;                  
    $('#rackBoxLi .list').append('<li id="listElement'+k+'">Device Added</li>');
    $('#rackBoxLi .list #listElement'+k).css({"height": newHeight + 'px'});                  
}

As you are going to use the rule for multiple elements, use a class instead: 当您将规则用于多个元素时,请改用一个类:

.listElement{
  background-color: #0099CC;
  border:1px dashed black;
  border-radius: 5px;
  width:180px;
  margin-bottom: 5px;
}

Instead of finding the element after you created it (which is why you find more than you want to), create the element and keep the reference to it: 创建元素后,不要查找该元素(这就是为什么要查找更多内容的原因),而是创建该元素并保留对其的引用:

for(k=0; k<JSONObj.rackData[rackIdToBeDisplayed].groups.length; k++){                   
  newHeight = JSONObj.rackData[rackIdToBeDisplayed].groups[k] * 26;                  
  var el = $('<li>').addClass('listElement').text('Device Added');
  $('#rackBoxLi .list').append(el);
  el.css({"height": newHeight + 'px'});                  
}

As whiterabbit25 answered 正如whiterabbit25回答

Change code from 更改代码

 for(k=0; k<JSONObj.rackData[rackIdToBeDisplayed].groups.length; k++){                   
 newHeight = JSONObj.rackData[rackIdToBeDisplayed].groups[k] * 26;                  
 $('#rackBoxLi .list').append('<li id="listElement">Device Added</li>');
 $('#rackBoxLi .list #listElement').css({"height": newHeight + 'px'});                  
 }

To below code 到下面的代码

for(k=0; k<JSONObj.rackData[rackIdToBeDisplayed].groups.length; k++){                   
 newHeight = JSONObj.rackData[rackIdToBeDisplayed].groups[k] * 26;                  
 $('#rackBoxLi .list').append('<li class="listElement">Device Added</li>');
 $('#rackBoxLi .list .listElement').eq(k).css({"height": newHeight + 'px'});                  
 }

If you don't want to use class or id then 如果您不想使用class或id,那么

for(k=0; k<JSONObj.rackData[rackIdToBeDisplayed].groups.length; k++){                   
 newHeight = JSONObj.rackData[rackIdToBeDisplayed].groups[k] * 26;                  
 $('#rackBoxLi .list').append('<li>Device Added</li>');
 $('#rackBoxLi .list li').eq(k).css({"height": newHeight + 'px'});                  
 }

If .eq() doesn't works for you then use .last() 如果.eq()对您不起作用,请使用.last()

$('#rackBoxLi .list li').last().css({"height": newHeight + 'px'});

Hope this helps.. 希望这可以帮助..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM