如何在循环中运行上一个/下一个按钮?

时间:2018-09-30 05:40:36

标签: javascript jquery xml

我在下面的代码中找到了循环弹出窗口。它为我工作。之后,我添加了用于自动到期XML节点的代码。当代码遇到到期节点时,弹出窗口将停止循环。我该如何实现代码。

这是代码:-

data.xml

<cd expires="30/09/2018">
  <head>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</head>
  <image>https://upload.wikimedia.org/wikipedia/commons/thumb/5/52/Liliumbulbiferumflowertop.jpg/220px-Liliumbulbiferumflowertop.jpg</image>
</cd>

<cd expires="10/09/2018">
  <head>posuere lacus in, accumsan nulla.</head>
  <image>https://i.ebayimg.com/images/g/SpMAAOSwceNZXpSZ/s-l300.jpg</image>
</cd>

要循环弹出,我使用了以下代码

let nextIdx = idx + 1;
let prevIdx = idx - 1;
//to make cyclic
nextIdx = nextIdx == total ? 0 : nextIdx;
prevIdx = prevIdx == -1 ? (total -1) : prevIdx;

要使XML元素过期,我使用了以下代码

let expireArray = $(this).attr('expires').split('/');
        const expireDate = `${expireArray[2]}${expireArray[1]}${expireArray[0]}`;
        const now = new Date(),
            nowDate = `${now.getFullYear()}${(now.getMonth()+1) <10 ? '0'+(now.getMonth()+1): (now.getMonth()+1)}${now.getDate()}`;

        if (nowDate > expireDate) {
            return;
        }  

  //..........Please check plunker code

http://next.plnkr.co/edit/GmYtTezOO8Kr1rS2?open=lib%2Fscript.js&preview

1 个答案:

答案 0 :(得分:0)

导航到元素时,该元素已被nowDate > expireDate-popups are stop looping过滤。

prevIdx/nextIdx/total-应从filtered个元素计算得出。

xml = $(xml).children();

const filteredChildren = $(xml).children()
.filter(function (el){
    /* CODE */
    return nowDate <= expireDate; 
    })

let total = filteredChildren.length;

filteredChildren.each(function (idx) {        
    let tag = $(this).prop("tagName");
    let nextIdx = idx + 1;
    let prevIdx = idx - 1;
    //to make cyclic
    nextIdx = nextIdx == total ? 0 : nextIdx;
    prevIdx = prevIdx == -1 ? (total -1) : prevIdx;

    /* CODE */
}

Plunker