我不能把课程添加到n-child

时间:2016-06-29 11:06:55

标签: javascript

HTML:

if (Control != null) {
   Control.BorderStyle = UITextBorderStyle.None;
   Control.TextAlignment=UITextAlignment.Center;
   UIImage img=UIImage.FromFile("Images/newimg.jpg");
   Control.Background=img
}

<div id="brojac-poteza" style="clear:both;padding-top:40px;padding-left:60px;padding-right:40px"> <div class="round-end darker-back card-choice">1</div> <div class="darker-back card-choice">2</div> <div class="darker-back card-choice">3</div> <div class="darker-back card-choice">4</div> <div class="darker-back card-choice">6</div> <div class="darker-back card-choice">5</div> <div class="darker-back card-choice">7</div> <div class="darker-back card-choice">8</div> <div class="darker-back card-choice">9</div> <div class="darker-back card-choice">10</div> </div> (在此代码上方设置script.js

var g = 1;

我测试时说g += 1; $('#brojac-poteza:nth-child(g)').addClass('round-end');

4 个答案:

答案 0 :(得分:1)

将其更改为

$('#brojac-poteza:eq('+g+')').addClass('round-end');

答案 1 :(得分:1)

首先,你需要使用变量g而不是g作为字符串,并找到不在root上的第n个孩子:

&#13;
&#13;
var g = 5;
$('#brojac-poteza div:nth-child(' + g + ')').addClass('round-end');
&#13;
.round-end {
  background-color: red;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="brojac-poteza" style="clear:both;padding-top:40px;padding-left:60px;padding-right:40px">
  <div class="round-end darker-back card-choice">1</div>
  <div class="darker-back card-choice">2</div>
  <div class="darker-back card-choice">3</div>
  <div class="darker-back card-choice">4</div>
  <div class="darker-back card-choice">6</div>
  <div class="darker-back card-choice">5</div>
  <div class="darker-back card-choice">7</div>
  <div class="darker-back card-choice">8</div>
  <div class="darker-back card-choice">9</div>
  <div class="darker-back card-choice">10</div>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您也可以尝试这些解决方案: -

$('#brojac-poteza .darker-back:nth-child(' + g + ')').addClass('round-end');

它可能对你有帮助。

答案 3 :(得分:0)

您还可以尝试计算.darker-black元素,例如:

$(function() {
    var numItems = $('#brojac-poteza .darker-back').length; /* Count items */
    var odd = 2; /* Change the steps here if you like to */

    /* Now loop it, and skip 1 step, 1, 3, 5 ... */
    for(g = 1; g < numItems; g+=odd) {
        $('#brojac-poteza div:nth-child(' + g + ')').addClass('test');
        console.log(g);
    }
});

看到这个小提琴:https://jsfiddle.net/r2ncLpLb/1/

我希望这对你有用:)