gpt4 book ai didi

javascript - 如何使用 jQuery 获取元素的文本兄弟?

转载 作者:行者123 更新时间:2023-11-30 11:51:46 25 4
gpt4 key购买 nike

我正在抓取一个网站,我发现了这个

<table>
<tr>
<td>
<b>Status:</b>ACTIVE;
<b>Type:</b>CN - CONSTRUCTION
<b>Added:</b>02/24/2012
</td>
</tr>
</table>

如何分别获取状态类型添加

我知道我会被否决,因为我没有发布任何尝试过的代码……但我什至想不出要尝试什么!

这个网站的 HTML 结构很差,我似乎找不到任何方法。

最佳答案

  • 使用 jQueryElement.text() 获取所有文本。
  • 使用String#spplit分割字符串

var text = $('#content').text();
var split = text.trim().split('\n');
split.forEach(function(el) {
var splitAgain = el.split(':');
console.log("Key: " + splitAgain[0].trim() + " Value: " + splitAgain[1].trim());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<table>
<tr>
<td id="content">
<b>Status:</b>ACTIVE;
<b>Type:</b>CN - CONSTRUCTION
<b>Added:</b>02/24/2012
</td>
</tr>
</table>

关于javascript - 如何使用 jQuery 获取元素的文本兄弟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39233736/

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