如何更改div类中的数字并进行更新?

时间:2019-04-13 22:30:31

标签: javascript html

我正在尝试更改网站上的现有HTML代码。请参见下面的示例:

var multiplier = 1.5;
const formatter = new Intl.NumberFormat('en-US', {
    style: 'currency',
    currency: 'USD',
    minimumFractionDigits: 0
})
        
//for first two occurences
var priceEls = document.getElementsByClassName("price");
for (var i = 0; i < priceEls.length; i++) {
    var price = priceEls[i].innerText;
    var priceN = getNumber(price)*multiplier;
    alert("Price: " + formatter.format(priceN));
}
//for last occurence
var priceEls = document.getElementsByClassName("column--container value-item");
for (var i = 0; i < priceEls.length; i++) {
    var price = priceEls[i].innerText;
    //alert("Price: " + price);
    var priceN = getNumber(price)*multiplier;
    alert("Price: " + formatter.format(priceN));
}
        
function getNumber(myString) {
    var numb = myString.match(/\d/g);
    numb = numb.join("");
    //alert (numb);
    return(parseInt(numb));
}
<div class="price">$2,000</div>
<div class="price">
    <span>$</span>
    3,000
</div>
<div class="column--container value-item">
    <span class="text-semibold-xs">Price</span>
    $1,500                  
</div>

如何使用这些新值更新HTML代码?

1 个答案:

答案 0 :(得分:0)

计算新价格后,您可以做的一件事就是将元素 public static void insertLast(LinkedList<Person> list, Person data) { if (list.Count == 0) { list.AddFirst(data); } else { list.AddAfter(list.Last, data); } } 设置为警报中使用的文本。因此,您的一个循环可能看起来像这样

  //Check if the task exist for the task date

  var listoftasks = Tasks.Tasks.list(tasklistID)

  var filtermonth="0" + shTaskStartMonth

  var filterdate=scTaskStartYear + "-" + filtermonth.substr(filtermonth.length-2) + "-" + shTaskStartDay + "T00:00:00.000Z"

  var filteredtask=listoftasks["items"].filter(function(item){

      return item["due"]== filterdate && item["title"]===Title

    })

    if(filteredtask.length==0){

        //Create a new task
        var task = Tasks.newTask()

        //Set the task parameters
        task.title = Title;
        task.due=new Date(scTaskStartYear,scTaskStartMonth,scTaskStartDay).toISOString()

        //add task to list
        task = Tasks.Tasks.insert(task, tasklistID)

    }

    else{

        //Get the existing task
        task = Tasks.Tasks.get(tasklistID, filteredtask[0].id)

        task.setStatus("completed")
    }

我建议使用innerText而不是for (var i = 0; i < priceEls.length; i++) { var price = priceEls[i].innerText; var priceN = getNumber(price)*multiplier; priceEls[i].innerText = "Price: " + formatter.format(priceN); } 进行循环。