根据子元素的字符串匹配创建父元素文本的数组

时间:2016-11-28 23:04:19

标签: javascript jquery html

尝试搜索大量成员列表(1200+)并找到我的哪些管理员添加了每个成员。我希望控制台返回由X添加的所有成员的名称数组,其中X是管理员的名称。这样我就可以编译一个电子表格来跟踪相应管理员添加的成员。

示例 HTML 我正在使用。

<div class="_6a _5u5j _6b">
  <div class="fsl fwb fcb"><a href="">Jessica Smith</a></div>
  <div class="_5aj7">
    <div class="_4bl9">
      <div class="_17tq">Model at Model/Actress</div>
      <div class="fsm fwn fcg">Joined
        <br>Added by Jane Doe <abbr title="Sunday, July 17, 2016 at 1:29pm" data-utime="1468772964" class="timestamp"><span class="timestampContent">about 4 months ago</span></abbr></div>
    </div>
    <div class="_4bl7 mrm"></div>
  </div>
</div>

<div class="_6a _5u5j _6b">
  <div class="fsl fwb fcb"><a href="">Robyn Stevens</a></div>
  <div class="_5aj7">
    <div class="_4bl9">
      <div class="_17tq">No Job</div>
      <div class="fsm fwn fcg">Joined
        <br>Added by Jane Doe <abbr title="Sunday, July 17, 2016 at 1:29pm" data-utime="1468772964" class="timestamp"><span class="timestampContent">about 2 months ago</span></abbr></div>
    </div>
    <div class="_4bl7 mrm"></div>
  </div>
</div>

<div class="_6a _5u5j _6b">
  <div class="fsl fwb fcb"><a href="">Sarah West</a></div>
  <div class="_5aj7">
    <div class="_4bl9">
      <div class="_17tq">Entertainer</div>
      <div class="fsm fwn fcg">Joined
        <br>Added by Jane Doe <abbr title="Sunday, July 17, 2016 at 1:29pm" data-utime="1468772964" class="timestamp"><span class="timestampContent">about 3 months ago</span></abbr></div>
    </div>
    <div class="_4bl7 mrm"></div>
  </div>
</div>

这是 jQuery 我试图在控制台中执行

$("div.fsm:contains('Jane Doe')").find("div.fsl a").each(function() {
  console.log($(this).text());
})

但我在这里没有取得任何进展。这是一个小提琴,我在https://jsfiddle.net/w9vfq62r/3/玩弄任何帮助,让这个小提琴示例正常工作将不胜感激。

3 个答案:

答案 0 :(得分:1)

在这里,您将很好地解析所有管理员,并将相应的成员附加到管理数组中。

也更新了时间戳。

var admins = {};
var refText = 'Added by';
$('div.fsm').each(function() {
    var self = $(this);
    var addedByText = self.text().replace('\n', ' ').replace(/\s{2,}/, ' ');
    var startIndex = addedByText.indexOf(refText) + refText.length;
    var admin = $.trim(addedByText.substring(startIndex, addedByText.indexOf('about')));
    var member = self.closest('._6a').find('div.fsl a').text();
  
    if(!admins[admin]) {
      admins[admin] = [];
    }
  
    admins[admin].push(member);
    admins[admin].push(self.find('abbr').attr('title'));
});

console.log(admins);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="_6a _5u5j _6b">
  <div class="fsl fwb fcb"><a href="">Jessica Smith</a></div>
  <div class="_5aj7">
    <div class="_4bl9">
      <div class="_17tq">Model at Model/Actress</div>
      <div class="fsm fwn fcg">Joined
        <br>Added by Jane Doe <abbr title="Sunday, July 17, 2016 at 1:29pm" data-utime="1468772964" class="timestamp"><span class="timestampContent">about 4 months ago</span></abbr></div>
    </div>
    <div class="_4bl7 mrm"></div>
  </div>
</div>

<div class="_6a _5u5j _6b">
  <div class="fsl fwb fcb"><a href="">Robyn Stevens</a></div>
  <div class="_5aj7">
    <div class="_4bl9">
      <div class="_17tq">No Job</div>
      <div class="fsm fwn fcg">Joined
        <br>Added by Jane Doe <abbr title="Sunday, July 17, 2016 at 1:29pm" data-utime="1468772964" class="timestamp"><span class="timestampContent">about 2 months ago</span></abbr></div>
    </div>
    <div class="_4bl7 mrm"></div>
  </div>
</div>

<div class="_6a _5u5j _6b">
  <div class="fsl fwb fcb"><a href="">Sarah West</a></div>
  <div class="_5aj7">
    <div class="_4bl9">
      <div class="_17tq">Entertainer</div>
      <div class="fsm fwn fcg">Joined
        <br>Added by Jane Doe <abbr title="Sunday, July 17, 2016 at 1:29pm" data-utime="1468772964" class="timestamp"><span class="timestampContent">about 3 months ago</span></abbr></div>
    </div>
    <div class="_4bl7 mrm"></div>
  </div>
</div>

答案 1 :(得分:0)

$("div.fsm:contains('Jane Doe')").each(function() {
    var txt = $(this).closest("._6a").find("div.fsl a").text();
    console.log(txt);
});

过滤所需的div,然后按closest获取包装,然后获取链接文本

答案 2 :(得分:0)

将其作为功能

demo

var memberss = getMembers("Jane Doe"); // function call to get array of members
alert(memberss);

function getMembers(adminName){
  var members = [];
  $('div.fsm:contains("'+adminName+'")').each(function() {
    var aMember = $(this).closest('._6a').find('.fsl a').text();
    members.push(aMember);
  });
  return members;
}