如何将赔率和均衡结合到javascript中的主要ID中?

时间:2017-09-01 17:38:38

标签: javascript

我有元素列表oddsevens。我希望将所有赔率和平均值合并到ID main删除节点oddsevens

我试图像这样实现它:



len = $('#odds').children().length + $('#evens').children().length;

for (var k = 1; k < len + 1; k++) {
  if (k % 2 != 0) {
    console.log('odds:', $('#odds').children()[0]);
    $('#main').append($('#odds').children()[0]);
  } else if (k % 2 == 0) {
    console.log('evens', $('#evens').children()[0]);
    $('#main').append($('#evens').children()[0])
  }
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="main">

</div>

<div id="odds">
  <div>sentence number one</div>
  <div>sentence number three</div>
  <div>sentence number five</div>
  <div>sentence number seven</div>
</div>

<div id="evens">
  <div>sentence number two</div>
  <div>sentence number four</div>
  <div>sentence number six</div>
</div>
&#13;
&#13;
&#13;

我能够获得输出,但问题是我需要在交错后删除kill节点/元素oddsevens。我如何实现这一目标?

2 个答案:

答案 0 :(得分:2)

&#13;
&#13;
var odds = document.getElementById("odds").children;
var evens = document.getElementById("evens").children;
var main = document.getElementById("main");

var addOdd = true;

while(odds.length > 0 && evens.length > 0){
  if(addOdd){
  	main.appendChild(odds[0]);
  }
  else{
  	main.appendChild(evens[0]);
  }
  addOdd = !addOdd;
}


	
&#13;
<div id="main">

</div>

<div id="odds">
    <div>sentence number one</div>
    <div>sentence number three</div>
    <div>sentence number five</div>
    <div>sentence number seven</div>
</div>

<div id="evens">
    <div>sentence number two</div>
    <div>sentence number four</div>
    <div>sentence number six</div>
</div>
&#13;
&#13;
&#13;

注意:你应该先确保你有足够的平均值和赔率。

答案 1 :(得分:0)

认为这就是你要做的事情:

function combine(){
  var main  = document.getElementById('main');
  var odds  = document.getElementById('odds');
  var evens = document.getElementById('evens');

  var combined = odds.innerHTML + evens.innerHTML

  main.innerHTML = combined;
  odds.innerHTML = "";
  evens.innerHTML = "";
}
combine()