根据先前的类名有选择地隐藏链接
首先,我想请你不要向我推荐 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';
}
}
getElementsByClassName
和 hasClass
是我为此编写的函数。
到目前为止,一切都很完美。
现在,我想做的是,如果 title
链接之一也有一个 active
类,那么我不想隐藏它后面的链接。
例如,如果2ND TITLE
有一个active
类,那么我想显示2ND TITLE
的所有以下链接,但仍然隐藏1ST TITLE
和 3RD 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
保留最后传递的
title
链接是否处于活动状态的参考;如果是,那就不要隐藏,否则就隐藏。类似于:编辑 假设任何时候只有一个标题标记为活动,您可以使用纯 CSS 实现您正在寻找的内容:
如果第一个和最后一个都处于活动状态,则多个活动标题会失败(其他组合也有效)。此外,您的里程可能会因浏览器支持而异: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: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:
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
迭代时,跟踪当前链接是否位于活动链接下方: http://jsfiddle.net/8bwyU/ 1/。
When iterating, keep track of whether the current link is beneath an active one: http://jsfiddle.net/8bwyU/1/.