【发布时间】:2013-10-21 17:38:47
【问题描述】:
我有一组 DIV
一个 DIV 最初设置为没有 .hidden 属性,所有其他人都有它。
我有一组 HREF 的调用 DIV 将使其显示。
我有一个用于“隐藏显示:无;”的 css和“显示显示:块;”
我希望能够循环遍历所有 div,删除所选 div 的 .hidden 属性并放置 OLD div 的 .hidden 属性...
简单吧?我对 JQ 的了解是有限的,并且在路上颠簸......就像现在一样。
代码如下:
<section id='content'>
<div id='stuff1' class='somestuff hidden'>
<p>Hello World 1</p>
</div>
<div id='stuff2' class='somestuff'>
<p>Hello World 2</p>
</div>
<div id='stuff3' class='somestuff hidden'>
<p>Hello World 3</p>
</div>
<div id='stuff4' class='somestuff hidden'>
<p>Hello World 4</p>
</div>
<div id='stuff5' class='somestuff hidden'>
<p>Hello World 5</p>
</div>
</section>
这是资产净值:
<div id='nav'>
<a href='#' onclick='changePage(this.id)' id='stuff1'>Click for Content 1</a>
<a href='#' onclick='changePage(this.id)' id='stuff2'>Click for Content 2</a>
<a href='#' onclick='changePage(this.id)' id='stuff3'>Click for Content 3</a>
<a href='#' onclick='changePage(this.id)' id='stuff4'>Click for Content 4</a>
<a href='#' onclick='changePage(this.id)' id='stuff5'>Click for Content 5</a>
</div>
最后我相信是函数:
<script>
function changePage(currPage)
{
$(document).ready(function() {
$('#' + currPage + 'Page .contentBody').each(function(i, j) {
console.log(i); //the i
console.log(j); //the div
$('#' + currPage + 'Page' + "Page").removeClass('hiddenstuff');
$('#' + currPage + 'Page' + "Page").addClass('hiddenstuff');
});
});
}
</script>
是的,我知道这个功能无处不在,但本质上,我怎么知道用户会点击什么以及他们什么时候点击,他们刚刚离开了哪一个,他们要去哪一个???嗯嗯?
谢谢
【问题讨论】:
-
简单。他们点击的那个是点击事件所针对的那个,而上一个是没有 .hidden 类的那个。从你的锚标签中删除 id,它们不属于那里。
-
您不应该在同一个文档中有重复的
id值。它们必须始终是唯一的。 -
他的代码让我相信它们是独一无二的,注意在选择内容 div 时在 id 中添加了“Page”