清除javascript:确定点击了哪个链接

时间:2017-03-18 10:18:04

标签: javascript jquery

我有一个链接循环,我确定了它们的点击事件。我想定义if navbar[1].clicked == true {doing something} else if navbar[2].cliked == true {doing something}等等#34;如果在"揭示功能callbackFn"。

以下是代码:

var navbar = document.getElementById("navbar").getElementsByTagName("a");
for (var i = 0; i < navbar.length; i++) {
    navbar[i].addEventListener('click', function() { reveal('top'); });
}

function reveal(direction) {

        callbackFn = function() {

            // this is the part where is running the turning of pages
            classie.remove(pages[currentPage], 'page--current');

            if (navbar[1].clicked == true) {
                currentPage = 0;
            } else if(navbar[1].clicked == true) {
                currentPage = 1;
            } else if(navbar[2].clicked == true) {
                currentPage = 2;
            } else if(navbar[3].clicked == true) {
                currentPage = 3;
            } else if(navbar[4].clicked == true) {
                currentPage = 4;
            };

            classie.add(pages[currentPage], 'page--current');

        };
}

2 个答案:

答案 0 :(得分:0)

这通常是关闭的问题。

您可以进行以下更改

此处addEventListener的回拨功能是IIFE,&amp;在reveal函数中传递i

的值
var navbar = document.getElementById("navbar").getElementsByTagName("a");
for (var i = 0; i < navbar.length; i++) {
  navbar[i].addEventListener('click', (function(x) {
    reveal('top',x);
  }(i))};
}

在此功能中,您可以访问

    function reveal(direction,index) {

   // not sure what this function is mean by, but you will have the value of `i` which is denote the clicked element

        callbackFn = function() {

        // this is the part where is running the turning of pages
        classie.remove(pages[currentPage], 'page--current');

        if (index == 1) {
          currentPage = 0;
        } else if (index == 1) {
          currentPage = 1;
        } else if (index == 2) {
          currentPage = 2;
        } else if (index == 3) {
          currentPage = 3;
        } else if (index == 4) {
          currentPage = 4;
        };

        classie.add(pages[currentPage], 'page--current');

      };
    }

答案 1 :(得分:0)

以下是我的解决方案。

感谢brk在任何情况下提供帮助,再次感谢。

// determine clicked item
var n;
$('#navbar a').click(function(){
    if($(this).attr('id') == 'a') {
        n = 0;
    } else if($(this).attr('id') == 'b') {
        n = 1;
    } else if($(this).attr('id') == 'c') {
        n = 2;
    } else if($(this).attr('id') == 'd') {
        n = 3;
    } else if($(this).attr('id') == 'e') {
        n = 4;
    };
});

var pages = [].slice.call(document.querySelectorAll('.pages > .page')),
    currentPage = 0,

    revealerOpts = {
        // the layers are the elements that move from the sides
        nmbLayers : 3,
        // bg color of each layer
        bgcolor : ['#52b7b9', '#ffffff', '#53b7eb'],
        // effect classname
        effect : 'anim--effect-3'
    };
    revealer = new Revealer(revealerOpts);

// clicking the page nav
document.querySelector("#a").addEventListener('click', function() { reveal('cornertopleft'); });
document.querySelector("#b").addEventListener('click', function() { reveal('bottom'); });
document.querySelector("#c").addEventListener('click', function() { reveal('left'); });
document.querySelector("#d").addEventListener('click', function() { reveal('right'); });
document.querySelector("#e").addEventListener('click', function() { reveal('top'); });

// moving clicked item's `n` into the function
function reveal(direction) {
        var callbackTime = 750;

        callbackFn = function() {

            classie.remove(pages[currentPage], 'page--current');

            currentPage = n;

            classie.add(pages[currentPage], 'page--current');
        };

    revealer.reveal(direction, callbackTime, callbackFn);
}