循环遍历li项,并在每个li项中设置变量以执行不同的操作

Loop over li items and set variables to do different things in each li item

本文关键字:li 执行 变量 操作 设置 遍历 循环      更新时间:2023-09-26

我想在页面上找到每个特定的LI元素,通过它查找特定的类,拉出该类,改变它一点,然后将它们的值设置为变量。

同一页面上最多有12个li项目,每个项目都有不同的"was"价格值。我想获取'was'的价格并只在li元素中使用。

到目前为止我写的是:

js:

jQuery(document).ready(function () {
    jQuery("#productList ul.gridStyle li").each(function () {
        var wasPriceMinusWasRemoval = jQuery('#productList p.was').text().replace(/'u00A3/g, '');
        var wasPrice = wasPriceMinusWasRemoval.replace("Was ", "");
        console.log(wasPrice); // ==> "149.99Was 119.99" (should be just 149.99)
        // generate a specific tag using .attr() dependent on 
        // wasPrice value inside this li element
    });
});

问题是,从这里要去哪里?索引的使用?

尝试在.each()函数中使用this引用,

var wasPriceMinusWasRemoval = jQuery('p.was',this).text().replace(/'u00A3/g, '');