gpt4 book ai didi

需要 JavaScript 正则表达式

转载 作者:行者123 更新时间:2023-12-02 19:44:48 24 4
gpt4 key购买 nike

我有这段 HTML 代码:

<tr class="AttCardFooter" onMouseOver="this.style.backgroundColor='#E1EAFE'" onMouseOut="this.style.backgroundColor='Transparent'">...etc etc.. new lines etc.. <td title="Saldo+">33:33</td><td title="Saldo-">22:22</td> .. etc etc... 

我需要 JavaScript 正则表达式来获取这两个字段 33:33 和 22:22由于换行符,我尝试的所有操作都失败了。如果有人知道如何实现这一点,我将非常感激。

最佳答案

Don't use a regexp 。我猜你的一段 html 是某个元素的 innerHTMLouterHTML 。不要使用正则表达式解析 html,而是这样做:

var el = document.querySelector("tr.AttCardFooter"); // I guess you have that variable already
for (var i=0; i<el.cells.length; i++) {
var td = el.cells[i];
if (td.title == "Saldo+")
var positiveSaldo = td.innerText;
else if (td.title == "Saldo-")
var negativeSaldo = td.innerText;
}
alert("Voila:\n"+positiveSaldo+"\n"+negativeSaldo);

您可以使用您最喜欢的库 dom 函数来改进这一点。例如,在 jQuery 中,它会是这样的

 var el = $("tr.AttCardFooter");
var positiveSaldo = el.find('td[title="Saldo+"]').text();
var negativeSaldo = el.find('td[title="Saldo-"]').text();

关于需要 JavaScript 正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9982172/

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