在Javascript中是否可以将选择器作为函数参数传递?
我正在尝试开发一种针对特定应用程序的幻灯片。我对要显示的div的增量有一些问题。 我需要传递选择器及其位置,然后增加其位置 检查我需要执行的操作的代码: HTML: Javascript:在Javascript中是否可以将选择器作为函数参数传递?,javascript,css,jquery-selectors,css-selectors,selector,Javascript,Css,Jquery Selectors,Css Selectors,Selector,我正在尝试开发一种针对特定应用程序的幻灯片。我对要显示的div的增量有一些问题。 我需要传递选择器及其位置,然后增加其位置 检查我需要执行的操作的代码: HTML: 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;
//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 + ")")
在visible元素中添加一个类,并允许jQuery在单击时选择下一个元素来获取该类,怎么样
<div name="div-default" class="title-result active">
<?php
echo "$name";
?>
</div>
<script>
jQuery(document).ready(function() {
(function ($) {
$('#next').on('click',function(){
$('.active').removeClass('active').next().addClass('active');
});
})(jQuery);
});
</script>
jQuery(文档).ready(函数(){
(函数($){
$('#next')。在('单击',函数()上){
$('.active').removeClass('active').next().addClass('active');
});
})(jQuery);
});
然后添加css以仅显示带有“active”类的div
<style>
.title-result{display:none;}
.title-result.active{display:block;/*or whatever*/}
</style>
.title结果{显示:无;}
.title result.active{display:block;/*或其他*/}
是的,这是可能的,你试过了吗?是的,我试过了。。这里没用。我不知道如何发送我的div位置号([name^=div0]:第n个child(2);[name^=div0]:第n个child(3);[name^=div0]:第n个child(4))…谢谢你的回答,但是我如何在HTML代码中定义变量的值呢(我需要得到我的div的位置,但我还不能。嗯……基于什么的位置?我还认为你应该把它放到它自己的按钮处理程序中,这样你就可以使用this
的一个实例来跟踪div的索引并使用它。)。
<div name="div-default" class="title-result active">
<?php
echo "$name";
?>
</div>
<script>
jQuery(document).ready(function() {
(function ($) {
$('#next').on('click',function(){
$('.active').removeClass('active').next().addClass('active');
});
})(jQuery);
});
</script>
<style>
.title-result{display:none;}
.title-result.active{display:block;/*or whatever*/}
</style>