循环遍历 dom 对象数组并找到第一个匹配的

发布于 2024-11-18 19:48:11 字数 647 浏览 4 评论 0原文

我的 eform 顶部有一个导航栏,可以跳到下一个和上一个 div(页面/部分),但前提是该 div 可见。除非使用复选框激活,否则 div 是隐藏的。因此,导航栏上的下一个按钮需要始终将您带到下一个可用的 div。

以下代码适用于第一个按钮,但这些导航栏显示在每个部分的顶部,因此下一部分上有一个运行相同功能的下一个按钮(这不起作用),我正在努力在这里解释自己,所以请如果我没有道理就大声喊叫。这是我的代码。

function showNext(){

    var pages = [document.getElementById("page2"),document.getElementById("page3")];
    var next = ["page2marker","page3marker"];

    for (var i=0; i<pages.length; i++){
        if(pages[i].style.display == "block"){
            window.location.hash = next[i];
        }
    }
}

我可以修改此功能,使其适用于所有按钮吗?即始终导航到下一个可见的可用 div?我想我可能错过了一个技巧和一大堆信息,但看看你的想法,有什么想法吗?

非常感谢

I have a navigation bar at the top of my eform that skips to the next and previous div (page/section) but only if that div is visible. The divs are hidden unless activated with a checkbox. so the next button on the nav bar needs to work by always taking you to the next available div.

The following code works for the first button but these nav bars display at the top of each section so the next section has a next button on it running the same function (which doesn't work) i'm struggling to exlpain myself here so please shout if i'm not making sense. Here's my code.

function showNext(){

    var pages = [document.getElementById("page2"),document.getElementById("page3")];
    var next = ["page2marker","page3marker"];

    for (var i=0; i<pages.length; i++){
        if(pages[i].style.display == "block"){
            window.location.hash = next[i];
        }
    }
}

Can i amend this function so that it will work for all buttons. I.e by always navigating to the next available div that is visible? I think i've probably missed a trick and a whole load of info but see what you think, any ideas?

Many thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

懒猫 2024-11-25 19:48:11

您可以使用 jQuery 和 jQuery.next 函数来实现此目的 - http://api。 jquery.com/next/

对于这样的一组 html:

<nav><a class="showNext">Show Next</a></nav>
<div class="content" style="display:none">I'm hidden</div>
<div class="content">I'm Visible</div>

您可以使用如下内容:

$('.showNext').bind('click', function(e) {
    e.stopPropagation();
    e.preventDefault();
    window.location.hash = $(this).next('.content:visible').attr('id') + 'marker';
});

You can achieve this using jQuery with the jQuery.next function - http://api.jquery.com/next/.

For a set of html like this:

<nav><a class="showNext">Show Next</a></nav>
<div class="content" style="display:none">I'm hidden</div>
<div class="content">I'm Visible</div>

You could use something like:

$('.showNext').bind('click', function(e) {
    e.stopPropagation();
    e.preventDefault();
    window.location.hash = $(this).next('.content:visible').attr('id') + 'marker';
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文