[英]How to show an element and hide when click on another
I have a series of div elements which i trigger with the code below. 我有一系列的div元素,可以通过下面的代码触发。
$(".course_id").on("click", function(){
var id = $(this).data("id");
$("div#lessons_by_course_" + id).removeClass("hidden");
});
The divs have an id called lessons_by_course + id eg div的id为lessons_by_course + id,例如
<div id="lessons_by_course1"></div>
the id number is parsed dynamically using ruby on rails .each loop. 使用rails .each循环上的ruby动态解析id号。
what i want is when i click on a div i want it to show and when i click on another div,i want jquery to hide the last one that was shown and show the one i clicked next. 我想要的是当我单击一个div时要显示的内容,而当我单击另一个div时,我想要jquery隐藏显示的最后一个并显示我接下来单击的那个。
Note that .course_id is a class for many elements so,each time i click on a certain one, a different div pops up because the id is different each time. 请注意,.course_id是一个包含许多元素的类,因此,每次单击某个特定元素时,都会弹出一个不同的div,因为id每次都不同。
You could cache the id of the last shown element in a global variable: 您可以将最后显示的元素的ID缓存在全局变量中:
var id;
Then in your loop: 然后在您的循环中:
$(".course_id").on("click", function(){
if(id) {
$("div#lessons_by_course_" + id).addClass("hidden");
}
id = $(this).id;
$(this).removeClass("hidden");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.