English 中文(简体)
两个高纬度的ULs
原标题:Equalize two ULs by height

我有一个案例,即Im提供了一台UL和数量不详的LL。 I m 期望利用联合材料将低收入国家分成2 ULs(I m 采用jQuery)。 我能以连线的数量来平均分出红灯,但我想根据每个电离层的高度而分立,这样,两个电离层都接近同一高度。

Any help with this would be appreciated, I don t feel like I m getting anywhere with the approach I started with.

感谢。

EDIT: 第一份联合材料。 超文本只是直截了当的UL/LI,每种电离层可能不同。

var $sections = $( div.subsection );

$sections.each(function(){
  var $section = $(this);

  var $list = $section.children( ul );
  var $items = $list.children( li );
  var itemCount = $items.size();
  var leftover = itemCount % 2;
  var itemsPerColumn = Math.floor(itemCount / 2);
  var $newList = $( <ul /> );

  $items.each(function(){
    var $this = $(this);
    var index = $items.index($this);

    if (index >= (itemsPerColumn + leftover)) {
      $this.remove().appendTo($newList);
    }
  });

  $list.after($newList);

  _equalizeListHeights();

  function _equalizeListHeights(){
    var listHeight = $list.height();
    var newListHeight = $newList.height();

    if (listHeight > newListHeight){
      var $lastItem = $list.children( li:last );
      var lastItemHeight = $lastItem.height();

      if (listHeight - lastItemHeight > newListHeight + lastItemHeight){
        $lastItem.remove().prependTo($newList);
        _equalizeListHeights();
      }
    }
  }

});
最佳回答
问题回答

www.un.org/Depts/DGACM/index_spanish.htm 欧安会问题,你正在努力解决。

You should look into the Backpack Problem. The items to be inserted into the backpack will be the LIs. Each LI will have a weight and value equivalent to its height. The backpack capacity should be half the sum of all the LI heights. Run an algorithm to solve the backpack problem and your LIs will be divided into two sets with heights as you ve described.

<>说明>

背包算法找到了一组物品,其价值尽可能大,但重量不超过背包能力。 但是,我们每个低收入国家的重量和价值是高的,后包能力是所有低收入国家合计总高的一半。

因此,从本质上来说,它会给我们带来一系列的全部生命线,这样,最高点尽可能高,不超过总高度1/2。 如果你最终拥有两套平等生活设施,这套设备就是其中之一。 如果你最后用两套不同高度的低收入人口组成,则后包装问题的解决办法将随着较小的高度而回到定点上(其余部分是非同学的劳动力)。

Solution

Try thecode used here: http://rosettacode.org/wiki/Knapsack_problem/Bounded#Javagust/a>

或者,如果你想推出你自己的话(不是建议的话):

$("ul.first li").each(function() {
    if ($("ul.first").outerHeight() > $("ul.second").outerHeight()) {
        $(this).appendTo($("ul.second"));
    }
});

采用这一分类法。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签