我有一个 jquery 函数,当单击带有 idtoggle_(某个唯一数字)的 anchor 标记时,它会显示一个 div。
$("a[id ^= 'toggle']").click(function(event){
event.preventDefault();
$("div [id ^= 'replypost']").toggle();
});
每个 id
toggle
和"replypost
的每个 id 都以`_(唯一数字)结尾,这样我就可以将所有的切换和回复帖子 div 分开.切换和回复帖子 div 通过 while() 循环。因此有多个
Toggle
和replypost
div,但每个都有唯一的编号。因此有一个
toggle_1
和一个ReplyPost_1
。
我需要一种方法,以便在单击 Toggle_1
时仅显示 ReplyPost_1
,在单击 Toggle_2
时仅显示 ReplyPost_2
, 这可能吗?如果您需要我解决任何问题,请告诉我并感谢您的宝贵时间。
最佳答案
仅显示具有匹配数字的帖子并隐藏其他帖子。
$("a[id^='toggle']").click(function(event){
event.preventDefault();
$("[id^='replypost']").hide();
$("#replypost_" + this.id.replace('toggle_', '') ).show();
});
但是如果这些帖子是 sibling ,则写得更少:
$("a[id^='toggle']").click(function(event){
event.preventDefault();
$("#replypost_" + this.id.replace('toggle_', '') ).show().siblings().hide();
});
关于javascript - JQuery仅显示某些id Div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14992133/