jquery很新,所以这可能是一个“真正的男人”类型的问题。但是我说我有两个函数要合并为一个,但是有一些细微的差别,例如next()和prev()方法,并且每个函数中的第一个和最后一个伪选择器是不同的:
function rotateImage(){
var currentPhoto = $("#photo_slideshow .current");
var nextPhoto = currentPhoto.next();
if(nextPhoto.length == 0){
nextPhoto = $("#photo_slideshow div:first");
}
}
function rotateImage2(){
var currentPhoto = $("#photo_slideshow .current");
var nextPhoto = currentPhoto.prev();
if(nextPhoto.length == 0){
nextPhoto = $("#photo_slideshow div:last");
}
}
必须有一种方法来组合这两个函数来压缩代码。谢谢你的帮助!
答案 0 :(得分:0)
你需要在function.something中提供一些参数,以确定是调用next还是调用next。这样的事情。
function rotateImage(side){
var currentPhoto = $("#photo_slideshow .current");
if(side=="next"){
var nextPhoto = currentPhoto.next();
if(nextPhoto.length == 0){
nextPhoto = $("#photo_slideshow div:first");
}
if(side=="prev")
{
var nextPhoto = currentPhoto.prev();
if(nextPhoto.length == 0){
nextPhoto = $("#photo_slideshow div:last");
}
}
打电话时:
供下次使用时使用rotateImage("next")
rotateImage("prev")
答案 1 :(得分:0)
function rotateImage(which, pseudoSelector) {
var currentPhoto = $("#photo_slideshow .current");
var nextPhoto = currentPhoto[which]();
if(nextPhoto.length == 0){
nextPhoto = $("#photo_slideshow div:" + pseudoSelector);
}
}
rotateImage('next', 'first');
rotateImage('prev', 'last');
答案 2 :(得分:0)
function rotateImage(side){
var isNext = side == 'next';
var currentPhoto = $("#photo_slideshow .current");
var nextPhoto = isNext ? currentPhoto.next() : currentPhoto.prev();
if(nextPhoto.length == 0){
nextPhoto = $("#photo_slideshow " + (isNext ? "div:first" : "div:last"));
}}