根据先前的类名有选择地隐藏链接

发布于 2024-12-15 20:47:36 字数 1217 浏览 1 评论 0原文

首先,我想请你不要向我推荐 jQuery。我不使用 jQuery,我也不想在这种情况下使用它。

话虽这么说,让我们看看我是否可以描述我的目标。我有一个链接列表如下:

<a href="#" class="title">1ST TITLE</a>
<a href="#">Item 1</a>
<a href="#">Item 2</a>
... more items ...
<a href="#" class="title">2ND TITLE</a>
<a href="#">Item 1</a>
<a href="#">Item 2</a>
... more items ...
<a href="#" class="title">3RD TITLE</a>
<a href="#">Item 1</a>
<a href="#">Item 2</a>
... more items ...

依此类推(总列表有 77 个链接,包括标题链接)

现在,我隐藏所有非“标题”链接,如下所示:

var sideMenu = getElementsByClassName(document, 'sm');
var links = sideMenu[0].getElementsByTagName('a');
for (i = 0; i < links.length; i++) {
    if (!hasClass(links[i], 'title')) {
        links[i].style.display = 'none';
    }
}

getElementsByClassNamehasClass 是我为此编写的函数。

到目前为止,一切都很完美。

现在,我想做的是,如果 title 链接之一也有一个 active 类,那么我不想隐藏它后面的链接。

例如,如果2ND TITLE有一个active类,那么我想显示2ND TITLE的所有以下链接,但仍然隐藏1ST TITLE3RD TITLE 链接。

First of all, I would like to ask you not to refer me to jQuery. I do not use jQuery, neither I would like to use it in this case.

That being said, let's see if I can describe my goal. I have a list of links as follows:

<a href="#" class="title">1ST TITLE</a>
<a href="#">Item 1</a>
<a href="#">Item 2</a>
... more items ...
<a href="#" class="title">2ND TITLE</a>
<a href="#">Item 1</a>
<a href="#">Item 2</a>
... more items ...
<a href="#" class="title">3RD TITLE</a>
<a href="#">Item 1</a>
<a href="#">Item 2</a>
... more items ...

and so on ( the total list is 77 links, including the title ones )

now, i hide all the NOT 'title' links like so:

var sideMenu = getElementsByClassName(document, 'sm');
var links = sideMenu[0].getElementsByTagName('a');
for (i = 0; i < links.length; i++) {
    if (!hasClass(links[i], 'title')) {
        links[i].style.display = 'none';
    }
}

getElementsByClassName and hasClass are function I've written for this.

Up to here, everything works perfectly.

Now, what I would like to do, is, in case one of the title links has also an active class, then I dont want to hide the links that come after it.

For example, if the 2ND TITLE had an active class, then I would like to show all following links of 2ND TITLE, but still hide the 1ST TITLE and 3RD TITLE links.

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

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

发布评论

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

评论(2

凝望流年 2024-12-22 20:47:36

保留最后传递的 title 链接是否处于活动状态的参考;如果是,那就不要隐藏,否则就隐藏。类似于:

var sideMenu = getElementsByClassName(document, 'sm');
var links = sideMenu[0].getElementsByTagName('a');
var isActive = false;
for (i = 0; i < links.length; i++) {
    if (hasClass(links[i], 'title') && hasClass(links[i], 'active'))
        isActive = true;
    if (hasClass(links[i], 'title') && !hasClass(links[i], 'active'))
        isActive = false;
    if (!hasClass(links[i], 'title') && !isActive) {
        links[i].style.display = 'none';
    }
}

编辑 假设任何时候只有一个标题标记为活动,您可以使用纯 CSS 实现您正在寻找的内容:

a.title ~ a:not(.title) { display: none; }
a.title.active ~ a:not(.title) { display: inline; }
a.title.active ~ a.title:not(.active) ~ a:not(.title) { display: none; }

如果第一个和最后一个都处于活动状态,则多个活动标题会失败(其他组合也有效)。此外,您的里程可能会因浏览器支持而异:P

Keep a reference as to whether the last passed title link was active or not; if it was, then don't hide, otherwise do. Something like:

var sideMenu = getElementsByClassName(document, 'sm');
var links = sideMenu[0].getElementsByTagName('a');
var isActive = false;
for (i = 0; i < links.length; i++) {
    if (hasClass(links[i], 'title') && hasClass(links[i], 'active'))
        isActive = true;
    if (hasClass(links[i], 'title') && !hasClass(links[i], 'active'))
        isActive = false;
    if (!hasClass(links[i], 'title') && !isActive) {
        links[i].style.display = 'none';
    }
}

EDIT Providing there's only ever one title marked as active at any one time, you can achieve what you're looking for with pure CSS:

a.title ~ a:not(.title) { display: none; }
a.title.active ~ a:not(.title) { display: inline; }
a.title.active ~ a.title:not(.active) ~ a:not(.title) { display: none; }

Multiple active titles fails if the first and last are both active (other combinations work). Also, your mileage may vary depending on browser support :P

如梦亦如幻 2024-12-22 20:47:36

迭代时,跟踪当前链接是否位于活动链接下方: http://jsfiddle.net/8bwyU/ 1/

function hasClass(elem, someClass) { // don't know your implementation...
    return elem.className.indexOf(someClass) !== -1;
}

var sideMenu = document.body.getElementsByClassName('sm'),
    links = sideMenu[0].getElementsByTagName('a'),
    currentIsActive = false;

for (var i = 0; i < links.length; i++) {
    if (!hasClass(links[i], 'title')) { // not a title link
       if(!currentIsActive) { // only hide if not under an active title link
           links[i].style.display = 'none';
       }
    } else { // a title link
        currentIsActive = hasClass(links[i], "active");
        // update whether current is active or not
    }
}

When iterating, keep track of whether the current link is beneath an active one: http://jsfiddle.net/8bwyU/1/.

function hasClass(elem, someClass) { // don't know your implementation...
    return elem.className.indexOf(someClass) !== -1;
}

var sideMenu = document.body.getElementsByClassName('sm'),
    links = sideMenu[0].getElementsByTagName('a'),
    currentIsActive = false;

for (var i = 0; i < links.length; i++) {
    if (!hasClass(links[i], 'title')) { // not a title link
       if(!currentIsActive) { // only hide if not under an active title link
           links[i].style.display = 'none';
       }
    } else { // a title link
        currentIsActive = hasClass(links[i], "active");
        // update whether current is active or not
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文