HTML:
<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
(above this code I set var g = 1;
)
script.js(在此代码上面我设置了var g = 1;)
g += 1;
$('#brojac-poteza:nth-child(g)').addClass('round-end');
When I test it says Uncaught Error: Syntax error, unrecognized expression: :nth-child
当我测试它时显示Uncaught Error:语法错误,无法识别的表达式:: nth-child
4 个解决方案
#1
1
change it to
改为
$('#brojac-poteza:eq('+g+')').addClass('round-end');
#2
1
First you need to use variable g not g as string and find nth of children not on root:
首先,你需要使用变量g而不是g作为字符串,并找到不在root上的n个子节点:
var g = 5;
$('#brojac-poteza div:nth-child(' + g + ')').addClass('round-end');
.round-end {
background-color: red;
}
<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>
#3
0
You can try these solution also :-
您也可以尝试这些解决方案: -
$('#brojac-poteza .darker-back:nth-child(' + g + ')').addClass('round-end');
It may help you.
它可能会帮助你。
#4
0
You can also try to count the .darker-black
elements by doing this for example:
您还可以通过执行以下操作来尝试计算.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);
}
});
See this fiddle: https://jsfiddle.net/r2ncLpLb/1/
看到这个小提琴:https://jsfiddle.net/r2ncLpLb/1/
I hope this will work for you either :)
我希望这对你有用:)
#1
1
change it to
改为
$('#brojac-poteza:eq('+g+')').addClass('round-end');
#2
1
First you need to use variable g not g as string and find nth of children not on root:
首先,你需要使用变量g而不是g作为字符串,并找到不在root上的n个子节点:
var g = 5;
$('#brojac-poteza div:nth-child(' + g + ')').addClass('round-end');
.round-end {
background-color: red;
}
<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>
#3
0
You can try these solution also :-
您也可以尝试这些解决方案: -
$('#brojac-poteza .darker-back:nth-child(' + g + ')').addClass('round-end');
It may help you.
它可能会帮助你。
#4
0
You can also try to count the .darker-black
elements by doing this for example:
您还可以通过执行以下操作来尝试计算.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);
}
});
See this fiddle: https://jsfiddle.net/r2ncLpLb/1/
看到这个小提琴:https://jsfiddle.net/r2ncLpLb/1/
I hope this will work for you either :)
我希望这对你有用:)