【问题标题】:jQuery find td with class name and change the textjQuery 用类名查找 td 并更改文本
【发布时间】:2019-08-15 21:50:49
【问题描述】:

我正在尝试使用 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’);
}
});

我做错了什么?

For more understanding html structure

Specifically what value i'm trying to change

PS:我用的是tampermonkey

    // ==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:Manaren 回答后

// ==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");
            }
        }
    );
})();

【问题讨论】:

    标签: jquery replace find


    【解决方案1】:

    我会试试这个,我在浏览器中测试过它可以正常工作。

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

    问题:

    • 错误的引号字符

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

    • 导入可能存在一些问题

    【讨论】:

    • 更改了引号并按照您所说的添加。还是什么都没有。
    • 你在什么时候执行你的代码?是在正确加载 HTML 之后吗?就像您在警报示例中所做的那样。否则将此代码包装在 window.onload 或类似文件中。
    • 实际上,在页面正常加载之前我已经收到了这个警报。那我应该使用 setTimeout 吗?
    • 比 setTimeout 更好的是 $(document).ready( "that piece of code" )
    【解决方案2】:

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

    $("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>

    【讨论】:

    • 你能看一下tampermoney脚本吗?我改变了这种方式,仍然没有。
    • 添加了一个使用此脚本的工作警报示例
    • 没有。也许我应该在控制台中打印一些东西以便更好地理解问题。
    猜你喜欢
    • 2012-04-22
    • 2013-10-22
    • 2016-07-16
    • 2012-07-26
    • 1970-01-01
    • 1970-01-01
    • 2012-12-19
    • 1970-01-01
    • 2013-03-04
    相关资源
    最近更新 更多