繁体   English   中英

使用jquery获取多个元素的属性值。 然后用它们

[英]Using jquery to get attribute value for multiple elements. Then use them

我有以下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>

我的目标是使用jQuery为每个< span >查找ID值,然后使用该ID设置将触发的.click()事件。 在< div >元素上使用show()函数。

到目前为止,我有这个:

var clasid = $(".thumbnav").attr("id");

 $("#" + clasid).click(function () {
 $("." + clasid).show();
});

哪个有效,但仅适用于第一个< span >。

所以我想知道是否有一种方法可以使此脚本适用于所有范围,我阅读了jQuery文档,他们建议使用.map().each() ,但我一直无法弄清楚。

如果有人能给我一个关于如何隐藏显示新的< div >时激活的< div >的提示,那也很好。

谢谢!

您可以编写类事件并动态访问该元素ID:

$(".thumbnav").click(function(){

$("." + this.id).show(); // this.id will give you clicked element id

})

参见示例

您可以使用以下方法简单地绑定它们:

$(".thumbnav").click(function () {
 $('.'+this.id).show();
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM