gpt4 book ai didi

javascript - 如何使用 javascript 检索 html 中所有突出显示的单词

转载 作者:行者123 更新时间:2023-12-02 17:43:30 25 4
gpt4 key购买 nike

我有一个仅包含文本组件的 Div,并且一些文本已使用背景颜色 rgb(255,255,0) 突出显示。

我想要做的是获取此 div 中用颜色 rgb(255,255,0) 突出显示的任何内容,并使用 javascript 将它们转换为字符串数组。

这是 html 代码:

<html><head>
</head>

<body class="body">
<div style="height: 10px;"></div>
2009 BMW for $5,800.00
<br>
Manheim Auto-Assign on run 3
<br><span style="background-color: rgb(255, 255, 0);">
Condition</span> and <span style="background-color: rgb(255, 255, 0);">Documentation</span> issues
<div style="height: 10px;"></div>
Estimated Gross Profit of $15.00
<br>
Estimated Margin Variance of -42%
<br>
<br>
Offer of $5,775.00
<br>
Current Offer Variance of $825.00

</body></html>

现在,我想从这个html中检索文本ConditionDocumentation,因为它们是突出显示的单词,并将它们存储在数组中。任何人都可以帮助如何在 javascipt 中做到这一点吗?

最佳答案

LIVE DEMO

var hili = document.getElementsByTagName('span');
var hiliArray = [];

for(var i=0; i<hili.length; i++){
var that = hili[i];
var bg = that.style.backgroundColor.replace(/ /g,'');
if (bg=="rgb(255,255,0)"){
var purifiedText = that.innerHTML.replace(/^\s+|\s+$/g, '');
hiliArray.push(purifiedText);
}
}

console.log(hiliArray);// ["Condition","Documentation"]

关于javascript - 如何使用 javascript 检索 html 中所有突出显示的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21979324/

25 4 0
文章推荐: javascript url 值到多个字段
文章推荐: javascript - 为什么这个 ajax 没有返回预期的 javascript
文章推荐: javascript - 如何获取所有 div 中特定
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com