0

大家好,我对 jquery 切换脚本有疑问..

<a class="clickMe">Text 1</a>
</br>
 <div class="sri"> - This text will be toggled</div>

<a class="clickMe">Text 2</a>
</br>
<div class="sri"> - This text will be toggled 2</div>


$('a.clickMe').click(function () {
// find first of following DIV siblings
// with class "textBox" and toggle it

$(this).nextAll('div.sri:first').show();
$('div.sri:visible').not($(this).nextAll('div.sri:first')).toggle();
});

上面的脚本工作正常,请查看演示

但是现在我需要<div id在不改变脚本功能的情况下使用什么来代替<div class...我尝试了很多方法但仍然遇到一些问题...我可能不必使用<a class="clickMe">有没有办法解决这个问题?

4

2 回答 2

2

确保不要重复分配给元素的 ID。

以下是 XHTML 1.0 的一些规范:

在 XML 中,片段标识符属于 ID 类型,每个元素只能有一个 ID 类型的属性。因此,在 XHTML 1.0 中,id 属性被定义为 ID 类型。为了确保 XHTML 1.0 文档是结构良好的 XML 文档,XHTML 1.0 文档在上面列出的元素上定义片段标识符时必须使用 id 属性。有关在将 XHTML 文档作为媒体类型 text/html 提供服务时确保此类锚点向后兼容的信息,请参阅 HTML 兼容性指南。

于 2012-09-05T11:54:47.893 回答
0

具有唯一 ID:

<a class="clickMe" toggle="first">Text 1</a>
</br>
 <div id="first" class="text"> - This text will be toggled</div>

<a class="clickMe" toggle="second">Text 2</a>
</br>
<div id="second" class="text"> - This text will be toggled 2</div>


$('a.clickMe').click(function () {
   id = $(this).attr('toggle');
   $('.text').not('#' + id).hide();
   $('#' + id).show();
});

查看我的Demo或我编辑的Demo或第三个Demo

于 2012-09-05T11:55:33.573 回答