我正在尝试为特定应用程序开发一种幻灯片。我在增加要显示的 div 时遇到了一些问题。 我需要将选择器及其位置传递给它,从而增加它的位置。
检查我需要做的代码:
HTML:
<div name="div-default" class="title-result">
<?php
echo "$name";
?>
</div>
<div name="div01" id="div01" class="title-result">
<?php
echo "Seu número de Personalidade é: $numerologia[0]";
?>
</div>
<div name="div02" class="title-result">
<?php
echo "Seu número de Destino é: $numerologia[1]";
?>
</div>
<div name="div03" class="title-result">
<?php
echo "Seu número de Lição de Vida é: $numerologia[2]";
?>
</div>
<div>
<button onclick="mudarDivNext([name ^= div0]:nth-child(SEND_ITS_POSITION_HERE))" id="next" class="change-div"></button>
</div>
CSS:
//here i define that the first div is visible, and the next ones invisible
div[name ^= div0]:nth-child(2){
display: inherit;
}
div[name ^= div0]{
display: none;
}
Javascript:
//This script already works for a fix position (from 2 for 3, for example) - need to make it work for all the divs.
function mudarDivNext(){
var n = 2;
$("[name ^= div0]:nth-child("+n+")").css("display", "none");
$("[name ^= div0]:nth-child("+(n+1)+")").css("display", "inherit");
}
最佳答案
有几点,在使用内联函数调用时不要忘记引号,并使用字符串连接来发送参数:
mudarDivNext("[name ^= div0]:nth-child(" + variable + ")")
关于javascript - 是否可以在 Javascript 中将选择器作为函数参数传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23020032/