Javascript导航输出(缩小)

时间:2012-09-22 01:55:55

标签: javascript jquery

我有一个页面,我输出以下内容

这是我模板中的html

<ul id="process"><span>Process: </span>

这是我缩小的Javascript(functions.js)

$(window).load(function () {
    var a = $("#slider").children("img");
    currentIndex = 0;
    slideCount = a.length;
    fadeDuration = 1E3;
    navigation = $("#process");
    navCnt = 0;
    navNumFix = 1;
    for (i = 0; i < slideCount; i++) navigation.append('<li><a href="#" class="navItem" data="' + navCnt+++'">' + navNumFix+++"</a></li>&#32;");
    $(".navItem").click(function () {
        var b = $(this).attr("data"),
            c = currentIndex;
        currentIndex = b;
        c != currentIndex && ($(a[c]).fadeOut(fadeDuration), $(a[currentIndex]).fadeIn(fadeDuration))
    })
});

对于我的滑块,这个都输出ul标签并通过Javascript附加列表项,如下所示:

<ul id="process"><span>Process: </span>
<li><a href="#" class="navItem" data="0">1</a></li>
<li><a href="#" class="navItem" data="1">2</a></li> 
</ul>

不是在我的模板中输出ul标签,而是只有在脚本调用时才能输出ul标签?有点像上面的列表项。

1 个答案:

答案 0 :(得分:1)

这应该......首先将模板显示为none,然后在添加列表元素后显示:

<ul style="display:none" id="process"><span>Process: </span></ul>

Javascript:

$(window).load(function () {
    var a = $("#slider").children("img");
    currentIndex = 0;
    slideCount = a.length;
    fadeDuration = 1E3;
    navigation = $("#process");
    navCnt = 0;
    navNumFix = 1;
    for (i = 0; i < slideCount; i++) navigation.append('<li><a href="#" class="navItem" data="' + navCnt+++'">' + navNumFix+++"</a></li>&#32;");
    $(".navItem").click(function () {
        var b = $(this).attr("data"),
            c = currentIndex;
        currentIndex = b;
        c != currentIndex && ($(a[c]).fadeOut(fadeDuration), $(a[currentIndex]).fadeIn(fadeDuration))
    })

    // Display the ul if there are slides
    if(slideCount > 0)
       navigation.show();
});

编辑:添加条件以仅在有幻灯片时显示ul