gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 05:33:03 27 4
gpt4 key购买 nike

尝试搜索庞大的成员列表(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());
})

但我在这里没有取得任何进展。这是我正在玩的 fiddle https://jsfiddle.net/w9vfq62r/3/任何使这个 fiddle 示例正常工作的帮助将不胜感激。

最佳答案

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

也更新了时间戳。

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>

关于javascript - 根据子元素的字符串匹配创建父元素文本的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40855076/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com