简体   繁体   English

唯一影响同一个div类?

[英]Uniquely affect the same div class?

I have divs that have the exact same name. 我的div具有完全相同的名称。 On page load, I want to grab info from them like $('.myclass').attr("title"); 在页面加载时,我想从它们那里获取信息,例如$('.myclass').attr("title"); does, then do something to them, and make it output as the div content. 做,然后对他们做一些事情,并使其输出为div内容。

Basically like this JS FIDDLE where it grabs the title attribute and does something to it and outputs a result. 基本上像这样的JS FIDDLE ,它捕获 title属性并对其执行操作并输出结果。 Even though, they all have the same class name, they are treated as unique. 即使它们都具有相同的类名,它们也被视为唯一。

Specifically for this jQuery has .each method: 专门为此jQuery提供的.each方法:

$('.myclass').each(function() {
   var title = $(this).attr('title');
   // do something with it
});

Try this: http://jsfiddle.net/L2ypq1r7/ 试试这个: http : //jsfiddle.net/L2ypq1r7/

Loops over each timeago element, grabs the title and prepends to the div next to the formatted value. 循环遍历每个timeago元素,获取title并在格式化值旁边的div之前添加。

Demo 演示版

jQuery(document).ready(function() {
     $("abbr.timeago").timeago();

       $('.timeago').each(function() {
  var strTitle = $(this).attr('title');
   // do something with it
           if(strTitle === "2010-01-17T01:59:17Z"){ 
               $("#div1").html(strTitle);
           }
});
   });

Please check the JS fiddle above 请检查上面的JS小提琴

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

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