我正在尝试通过基于网络上现有的开放源代码制作自己的jQuery幻灯片学习。

我迷上了一大堆编码,我认为这主要是因为我一开始在编程/ jQuery方面不是很坚强,而且编写它的人也使用了一堆三元运算符(我相信这就是事实)。

有人可以帮我解决这个问题,甚至可以将三元运算符替换为更多的“传统”运算符吗?

//if no IMGs have the show class, grab the first image
var current = ($('ul.slideshow li.show') ?  $('ul.slideshow li.show') : $('#ul.slideshow li:first'));

//Get next image, if it reached the end of the slideshow, rotate it back to the first image
var next = ((current.next().length) ? ((current.next().attr('id') == 'slideshow-caption') ? $('ul.slideshow li:first') :current.next()) : $('ul.slideshow li:first'));


我对最后一行感到困惑。

最佳答案

我发现缩进是有帮助的。

var next = (
  (current.next().length) ? (
    (current.next().attr('id') == 'slideshow-caption') ?
      $('ul.slideshow li:first')
      : current.next()
    )
    : $('ul.slideshow li:first')
  );


相当于:

var next;
if (current.next().length) {
  if (current.next().attr('id') == 'slideshow-caption') {
    next = $('ul.slideshow li:first');
  } else {
    next = current.next();
  }
} else {
  next = $('ul.slideshow li:first');
}

09-11 18:16
查看更多