简体   繁体   English

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

[英]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.

相关问题 如何从多个元素的相同名称获取名称属性的索引。 - How to get index of name attribute from same name of multiple elements. 单击特定元素后,在Href属性内部改变一些查询值。 使用Jquery - Dinamically changing some Queries Values inside an Href Attribute after clicking on specific elements. Using Jquery jQuery ::图像元素列表。 试图使它们成为幻灯片 - jQuery::List of image elements. Trying to make them into a slideshow 如何使用jquery获取所有元素的数据属性值? - How to get data attribute value of all elements using jquery? jQuery,如何获取包含“ /”的数据属性值的元素并对其进行排序 - jQuery, how to get elements with data-attribute value that contains “/” and sort them jQuery选择具有名称属性的多个元素的属性值 - jQuery selecting attribute value of multiple elements with name attribute jQuery帮助页面和显示元素。 - Jquery help with pager and display elements. 如何使用jQuery获取大于给定值的元素的数据属性值? - How to get data-attribute value of elements which is greater than given value using jquery? 使用jQuery获取元素值 - Get elements value using jquery 如何获得所有可见的“标题”属性 <li> specefic中的元素 <ul> 使用jquery,在数组中收集主题并使用alert()显示它们; - how to get “title” attribute of all visible <li> elements in specefic <ul> using jquery, collect theme in an array and display them using alert();
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM