[英]Using jquery to get attribute value for multiple elements. Then use them
I have the following HTML: 我有以下HTML:
<span class="thumbnav" id="?"><?php get_the_image(); ?></span>
<span class="thumbnav" id="?"><?php get_the_image(); ?></span>
<div class="?" style="display:none;">
Some image, and a bit of text are staring at you.
</div>
<div class="?" style="display:none;">
Some image, and a bit of text are staring at you.
</div>
My goal is to use jQuery to find the ID value for every < span >, then use that ID to set a .click() event that will trigger a . 我的目标是使用jQuery为每个< span >查找ID值,然后使用该ID设置将触发的.click()事件。 show() function on the < div > elements. 在< div >元素上使用show()函数。
So far I have this: 到目前为止,我有这个:
var clasid = $(".thumbnav").attr("id");
$("#" + clasid).click(function () {
$("." + clasid).show();
});
Which works, but only for the first < span >. 哪个有效,但仅适用于第一个< span >。
So I was wondering if there is a way to make this script work for all the spans, I read the jQuery documentation and they suggest using either .map() or .each() , but I haven't been able to figure it out. 所以我想知道是否有一种方法可以使此脚本适用于所有范围,我阅读了jQuery文档,他们建议使用.map()或.each() ,但我一直无法弄清楚。
Also it would be great if anyone could give me a hint on how to hide a < div > that was active when a new < div > is being displayed. 如果有人能给我一个关于如何隐藏显示新的< div >时激活的< div >的提示,那也很好。
Thanks! 谢谢!
you can write class event and dynamically access that element id: 您可以编写类事件并动态访问该元素ID:
$(".thumbnav").click(function(){
$("." + this.id).show(); // this.id will give you clicked element id
})
See FIDDLE EXAMPLE 参见示例
You can simply bind them using: 您可以使用以下方法简单地绑定它们:
$(".thumbnav").click(function () {
$('.'+this.id).show();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.