繁体   English   中英

我如何在具有css类的特定TD中找到文本,其中TR存储为此

[英]How could i find the text in a specific TD with a css class where TR is stored as this

我用这个代码循环遍历tr

 $('#prod_tbl > tbody  > tr').each(function () {
                             var x = $(this);

              });

现在我想要的是我有不同类名的TD,如.price,.name

当我遍历TR时,我希望访问不同变量中每个TD的值

喜欢

     $('#prod_tbl > tbody  > tr').each(function () {
                                     var x = $(this);
var price=$("td.price").text();
var name= $("td.name").text();


                      });

对于每个TR

您必须在此上下文中使用.find()来完成您的任务。 那是因为td s是每个Tr的后代。 $(this)将指向Tr ,所以我们必须使用.find(selector)来查找匹配的后代,

尝试,

 $('#prod_tbl > tbody  > tr').each(function () {
    var x = $(this);
    var price= x.find("td.price").text();
    var name = x.find("td.name").text();
 });

请阅读此处以了解有关.find()更多信息

使用.find()来查找作为当前tr元素的后代的td

$('#prod_tbl > tbody  > tr').each(function () {
    var x = $(this);
    var price = $(this).find("td.price").text();
    var name = $(this).find("td.name").text();


});

您目前正在选择所有具有类别pricename的td。 在上下文中传递当前trjQuery(selector [,context])或使用find()获取当前行后代的td。

在选择器中使用上下文

var price=$("td.price", this).text();
var name= $("td.name", this).text();

使用find()

var price = $(this).find("td.price").text();
var name = $(this).find("td.name").text();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM