Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用jQuery遍历连接2个UL列表。_Jquery_List_Connect_Traversal - Fatal编程技术网

如何使用jQuery遍历连接2个UL列表。

如何使用jQuery遍历连接2个UL列表。,jquery,list,connect,traversal,Jquery,List,Connect,Traversal,我正试图在一个列表中实现下一个/上一个导航,由于页面设计,我不得不将其分为两个列表。我可以用jQuery在一个列表中轻松地完成这项工作,但我不知道如何从第一个列表中的最后一项跳转到第二个列表中的第一项,反之亦然。代码在这里:您可以检查这是否是最后一个li,然后跳转到下一个列表。跳到上一个的类似逻辑: 伪代码我稍后将尝试发布一个工作函数 $("#nav ul li a").bind("click keypress",function(e){ var dTitle=$(this).text(

我正试图在一个列表中实现下一个/上一个导航,由于页面设计,我不得不将其分为两个列表。我可以用jQuery在一个列表中轻松地完成这项工作,但我不知道如何从第一个列表中的最后一项跳转到第二个列表中的第一项,反之亦然。代码在这里:

您可以检查这是否是最后一个li,然后跳转到下一个列表。跳到上一个的类似逻辑:

伪代码我稍后将尝试发布一个工作函数

$("#nav ul li a").bind("click keypress",function(e){
    var dTitle=$(this).text();
    var dContentkey=$(this).attr("id");
                if($(this).parent().is(":last")) {
                     // get next ul item
                }
                else {
         nextItem=$(this).parent().next("li").find("a").attr("id");
                }
    testItem=$(this).parent("#nav").next("div ul li").find("a").attr("id");
    e.preventDefault();
    $("#currTitle").text("Clicked item: "+dTitle);
    $("#nextObjID").text("Next Item: "+nextItem);
    $("#testObjID").text("Test Item: "+testItem);
});

您可以检查这是否是最后一个li,然后跳转到下一个列表。跳到上一个的类似逻辑:

伪代码我稍后将尝试发布一个工作函数

$("#nav ul li a").bind("click keypress",function(e){
    var dTitle=$(this).text();
    var dContentkey=$(this).attr("id");
                if($(this).parent().is(":last")) {
                     // get next ul item
                }
                else {
         nextItem=$(this).parent().next("li").find("a").attr("id");
                }
    testItem=$(this).parent("#nav").next("div ul li").find("a").attr("id");
    e.preventDefault();
    $("#currTitle").text("Clicked item: "+dTitle);
    $("#nextObjID").text("Next Item: "+nextItem);
    $("#testObjID").text("Test Item: "+testItem);
});

如果您在当前列表中的第一次搜索未返回任何结果,您希望将其作为备选搜索:

nextItem = $(this)
    .closest("div") // find the closest div ancestor
    .next("div")    // then its next sibling div
    .find("ul li a")// then the first link in the list
    .attr("id");

如果您在当前列表中的第一次搜索未返回任何结果,您希望将其作为备选搜索:

nextItem = $(this)
    .closest("div") // find the closest div ancestor
    .next("div")    // then its next sibling div
    .find("ul li a")// then the first link in the list
    .attr("id");

首先,如果要对ID进行编号,如示例中所示,只需使用ID属性查找下一个或上一个项目

第二,这里有一个更普遍的解决方案。这适用于您的案例,但如果您在页面上有LIs、DIV或任何分散在随机位置的元素,并且您希望将所有单个元素链接在一起,以便可以向前和向后遍历它们,那么这也适用

步骤1:选择所需的所有元素。e、 g.给你$nav li 步骤2:迭代所有元素并将其索引号存储为数据值。 步骤3:当您单击所选元素时,检索其存储的数据编号,然后从索引位置检索元素,该位置比单击的项目小或大一个,以获取上一个或下一个元素。不要忘记循环结束。您只需使用步骤1和中的选择即可。就你而言。 点击an LI查看下一个

$(function() {
    // You can link lists anywhere on the page,
    // not just consecutive ones
    // create an object of all lis and store the index

    // Let's cache our big combine list
    $bigList = $("ul.big-list li");

    // Store the index value in the list item
    $bigList.each(function(index) {
        $(this).data("indexNum", index);
    });

    // Retrieve stored index value and add or subtract one
    $bigList.click(function() {
        // Add one to find next
        // Subtract one to find prev
        var nextIndex = $(this).data("indexNum") + 1;

        // Wrap from last to first
        if (nextIndex === $bigList.length)
            nextIndex = 0;

        // Find next or prev using .eq()
        $bigList.eq(nextIndex).animate(
                    {"font-size":"200%"},500).animate(     
                    {"font-size":"100%"},500);   
    });
});

首先,如果要对ID进行编号,如示例中所示,只需使用ID属性查找下一个或上一个项目

第二,这里有一个更普遍的解决方案。这适用于您的案例,但如果您在页面上有LIs、DIV或任何分散在随机位置的元素,并且您希望将所有单个元素链接在一起,以便可以向前和向后遍历它们,那么这也适用

步骤1:选择所需的所有元素。e、 g.给你$nav li 步骤2:迭代所有元素并将其索引号存储为数据值。 步骤3:当您单击所选元素时,检索其存储的数据编号,然后从索引位置检索元素,该位置比单击的项目小或大一个,以获取上一个或下一个元素。不要忘记循环结束。您只需使用步骤1和中的选择即可。就你而言。 点击an LI查看下一个

$(function() {
    // You can link lists anywhere on the page,
    // not just consecutive ones
    // create an object of all lis and store the index

    // Let's cache our big combine list
    $bigList = $("ul.big-list li");

    // Store the index value in the list item
    $bigList.each(function(index) {
        $(this).data("indexNum", index);
    });

    // Retrieve stored index value and add or subtract one
    $bigList.click(function() {
        // Add one to find next
        // Subtract one to find prev
        var nextIndex = $(this).data("indexNum") + 1;

        // Wrap from last to first
        if (nextIndex === $bigList.length)
            nextIndex = 0;

        // Find next or prev using .eq()
        $bigList.eq(nextIndex).animate(
                    {"font-size":"200%"},500).animate(     
                    {"font-size":"100%"},500);   
    });
});

这就是我要找的。我忘了,最近的,否则我想我会弄明白的。感谢您的快速回复,我还有一些头发要做,这就是我在上一篇文章中得到的结果。。previItem=$this.parent.prevli.finda.attrid;如果PreviItem{PreviItem=$this.closestdiv//查找最近的div祖先。Previdiv//然后是其上一个同级div.findul li:last a//然后是上一个列表中的最后一个链接。attrid;}很抱歉我上一条评论中的格式不正确,如果您有编号为=>nextItem=$a+1+parseFloat$this.attrid.substring1.attrid的ID,则这里有next/prev的完整代码;虽然样本中有编号的ID以便于工作,但现实世界中的东西却没有。这就是我想要的。我忘了,最近的,否则我想我会弄明白的。感谢您的快速回复,我还有一些头发要做,这就是我在上一篇文章中得到的结果。。previItem=$this.parent.prevli.finda.attrid;如果PreviItem{PreviItem=$this.closestdiv//查找最近的div祖先。Previdiv//然后是其上一个同级div.findul li:last a//然后是上一个列表中的最后一个链接。attrid;}很抱歉我上一条评论中的格式不正确,如果您有编号为=>nextItem=$a+1+parseFloat$this.attrid.substring1.attrid的ID,则这里有next/prev的完整代码;虽然样本中有编号的ID以便于工作,但现实世界中的东西却没有。