首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jQuery使用类名查找td并更改文本

jQuery使用类名查找td并更改文本
EN

Stack Overflow用户
提问于 2019-03-25 23:40:40
回答 2查看 11.9K关注 0票数 2

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

代码语言:javascript
复制
<td class="walletBalance">0.0000 XBT</td>

所以,我试着:

代码语言:javascript
复制
$("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’);
}
});

我做错了什么?

For more understanding html structure

Specifically what value i'm trying to change

PS: Im正在使用tampermonkey

代码语言:javascript
复制
    // ==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');
  }
});
})();

顺便说一句,这个闹钟起作用了:

代码语言:javascript
复制
// ==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: Manaren应答后

代码语言:javascript
复制
// ==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");
            }
        }
    );
})();
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-03-26 00:01:19

我会试一试,我在浏览器中测试了它,它工作正常。

代码语言:javascript
复制
$("tbody tr td.walletBalance").each(
        function(){
            if ($(this).text() == "0.0000 XBT"){
                $(this).text("changed text"); 
            }
        }
    );

问题:

  • 错误的引号字符
  • Imo对于如此简单的任务来说代码太复杂了
  • 导入

时可能会出现一些问题

票数 2
EN

Stack Overflow用户

发布于 2019-03-25 23:48:32

这个问题是因为您使用了无效的单引号和双引号字符。单引号应该是',而不是;双引号应该是",而不是。一旦解决了这个问题,你的代码就能正常工作:

代码语言:javascript
复制
$("tbody").find("tr").each(function() {
  var ratingTd = $(this).find('td.walletBalance');
  if (ratingTd.text() == "0.0000 XBT") {
    ratingTd.text('changed text');
  }
});
代码语言:javascript
复制
<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>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55341459

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档