jQuery找到带有类名的td并更改文本



我正在尝试使用jquery根据其类名查找文本并对其进行更改。基本上我有这个结构:

<td class="walletBalance">0.0000 XBT</td>

所以,我正在尝试:

$("tbody").find("tr").each(function() { //get all rows in table
var ratingTd = $(this).find('td.walletBalance’);//Refers to TD element
if (ratingTd.text() == “0.0000 XBT”) {
ratingTd.text(’changed text’);
}
});

我做错了什么?

为了进一步了解 html 结构

具体来说,我正在尝试更改什么值

PS:我使用篡改猴

// ==UserScript==
// @name         testingalarm
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://www.hey
// @grant        none
// @require http://code.jquery.com/jquery-latest.js
// ==/UserScript==
(function() {
'use strict';
// Your code here...
$("tbody").find("tr").each(function() {
var ratingTd = $(this).find('td.walletBalance');
if (ratingTd.text() == "0.0000 XBT") {
ratingTd.text('changed text');
}
});
})();

顺便说一句,这个警报正在工作:

// ==UserScript==
// @name         testingalarm
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://www.some
// @grant        none
// @require http://code.jquery.com/jquery-latest.js
// ==/UserScript==
(function() {
'use strict';
// Your code here...
$(document).ready(function() {
alert('WINNING');
});
})();

PSS:在马纳伦回答之后

// ==UserScript==
// @name         aaaaa
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://www.some
// @grant        none
// @require http://code.jquery.com/jquery-latest.js
// ==/UserScript==
(function() {
'use strict';
// Your code here...
$("tbody tr td.walletBalance").each(
function(){
if ($(this).text() == "0.0000 XBT"){
$(this).text("changed text");
}
}
);
})();

我会尝试这个,我在浏览器中对其进行了测试,它工作正常。

$("tbody tr td.walletBalance").each(
function(){
if ($(this).text() == "0.0000 XBT"){
$(this).text("changed text"); 
}
}
);

问题:

  • 引号字符错误

  • Imo 代码太复杂,无法完成如此简单的任务

  • 也许导入存在一些问题

问题是因为您使用了无效的单引号和双引号字符。单引号应该是',而不是,双引号需要",而不是。修复后,您的代码可以正常工作:

$("tbody").find("tr").each(function() {
var ratingTd = $(this).find('td.walletBalance');
if (ratingTd.text() == "0.0000 XBT") {
ratingTd.text('changed text');
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<td class="walletBalance">0.0000 XBT</td>
</tr>
</table>

最新更新