如何一次只运行一个函数

时间:2015-09-16 15:24:03

标签: jquery

我有以下代码,当页面的相应部分可见时,成功地将类添加到锚链接。问题是我不希望两者同时被激活。而是第二个项目仅在可见时激活,而前一个项目不激活。

我仍然是初学者,所以我感谢任何帮助。

    $(function () {
    var $itemone = $('#itemone');
    var testVis = function () {
        $itemone.each(function () {
            if ($(this).visible( true )) {
                $('#linkone').addClass('current');
            } else {
                $('#linkone').removeClass('current');
            }
        });
    };
    $(window).on('scroll resize', testVis);
    testVis();
});

$(function () {
    var $itemtwo = $('#itemtwo');
    var testVis = function () {
        $itemtwo.each(function () {
            if ($(this).visible( true )) {
                $('#linktwo').addClass('current');
            } else {
                $('#linktwo').removeClass('current');
            }
        });
    };
    $(window).on('scroll resize', testVis);
    testVis();
});

1 个答案:

答案 0 :(得分:0)

您可以在if中使用jquery的current方法来检查第一个是否具有类值if(! $("#linkone" ).hasClass( "current" )){ // make second one appear},然后使用它来显示/隐藏第二个

例如: - CREATE TABLE TEST2 ( JOB_NAME VARCHAR2(50) NULL, RUNTIME NUMBER(22) NULL, STARTTIME1 VARCHAR2(50) NULL, ENDTIME1 VARCHAR2(50) NULL, STARTTIME_READ DATE NULL, ENDTIME_READ DATE NULL ) GO insert into TEST2 values ('TEST JOB',37,'08/18/2015 20:12:24','08/18/2015 20:13:01',null,null) go insert into TEST2 values ('TEST JOB',37,'08/18/2015 20:12:24','08/18/2015 20:13:01',null,null) go insert into TEST2 values ('TEST JOB',195,'08/20/2015 19:17:05','08/20/2015 19:20:20',null,null) go insert into TEST2 values ('TEST JOB',171,'08/19/2015 19:16:52','08/19/2015 19:19:43',null,null) go insert into TEST2 values ('TEST JOB',195,'08/21/2015 19:19:08','08/21/2015 19:22:23',null,null) go insert into TEST2 values ('TEST JOB',32,'08/24/2015 19:23:52','08/24/2015 19:24:24',null,null) go insert into TEST2 values ('TEST JOB',19,'08/26/2015 19:19:00','08/26/2015 19:19:19',null,null) go insert into TEST2 values ('TEST JOB',92,'08/25/2015 19:18:59','08/25/2015 19:20:31',null,null) go insert into TEST2 values ('TEST JOB',198,'08/27/2015 19:29:58','08/27/2015 19:33:16',null,null) go insert into TEST2 values ('TEST JOB',19,'08/28/2015 19:18:13','08/28/2015 19:18:32',null,null) go insert into TEST2 values ('TEST JOB',44,'08/31/2015 19:49:22','08/31/2015 19:50:06',null,null) go insert into TEST2 values ('TEST JOB',266,'09/11/2015 19:17:10','09/01/2015 19:21:36',null,null) go insert into TEST2 values ('TEST JOB',18,'09/20/2015 19:17:33','09/02/2015 19:17:51',null,null) go insert into TEST2 values ('TEST JOB',33,'09/22/2015 19:16:50','09/03/2015 19:17:23',null,null) go insert into TEST2 values ('TEST JOB',170,'08/17/2015 19:18:07','08/17/2015 19:20:57