使用divid的Javascript切换
大家好,我对jquery切换脚本有问题使用divid的Javascript切换,javascript,jquery,Javascript,Jquery,大家好,我对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<
<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();
});
但是现在我需要使用的是,确保不要重复分配给元素的ID
以下是XHTML 1.0的一些规范:
在XML中,片段标识符是ID类型的,每个元素只能有一个ID类型的属性。因此,在XHTML 1.0中,id属性被定义为id类型。为了确保XHTML 1.0文档是结构良好的XML文档,XHTML 1.0文档在对上面列出的元素定义片段标识符时必须使用id属性。请参阅HTML兼容性指南,以获取有关在将XHTML文档作为媒体类型text/HTML提供时确保此类锚固件向后兼容的信息
使用唯一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();
});
或“我的编辑”或第三个。您可以更改html吗?因为ID必须是唯一的。ID应该是唯一的。你应该考虑这一点。如果你要删除类标签,那么你必须修改脚本(因为它在“类”标签上工作,而不是在ID上)。给我们看看ID-S的HTML。我肯定有一些误解。你可以绕过ID并检查下一个类元素:<代码> $(“a”)。单击(函数){if($)。nExcTunl(a))。.hasClass(“sri”){…
例如,但需求只是一个idI,我看不到该需求。但正如其他人提到的,使用同一ID两次(或多次)结果xml无效。谢谢您的回复jens…我正在尝试使用唯一id本身..但功能已更改..如果我单击text2..text1内容应隐藏,并且只显示text2内容..如何操作?它应与我使用唯一id显示的演示完全相同…然后您将需要一个类或完整的ID。但类是为此而创建的。我编辑我的答案…嗨,jens,但现在还有一个问题…如果我们第二次单击text2/text1,它将隐藏…但这不应该发生,也就是说,如果我单击text2,text1应该隐藏,如果我再次单击text2,结果应该相同,它不应该隐藏text2的内容。。