Pass variable in document.getElementByid in javascript

前端 未结 4 1166
有刺的猬
有刺的猬 2020-12-06 23:44

I have a variable account_number in which account number is stored. now i want to get the value of the element having id as account_number. How to do it in javascript ?

4条回答
  •  青春惊慌失措
    2020-12-06 23:58

    I found this page in search for a fix for my issue...

    Let's say you have a list of products:

    Western Digital 1TB

    Western Digital 1TB

    149.95

    add to cart
    Western Digital 1TB

    Western Digital 1TB

    139.95

    add to cart
    Western Digital 1TB

    Western Digital 1TB

    49.95

    add to cart

    The designer made all the prices have the digits after the . be superscript. So your choice is to either have the cms spit out the price in 2 parts from the backend and put it back together with tags around it, or just leave it alone and change it via the DOM. That's what I opted for and here's what I came up with:

    window.onload = function() {
    
        var pricelist = document.getElementsByClassName("rel-prod-price");
        var price_id = "";
        for (var b = 1; b <= pricelist.length; b++) {
            var price_id = "price_format_" + b;
            var price_original = document.getElementById(price_id).innerHTML;
            var price_parts = price_original.split(".");
            var formatted_price = price_parts[0] + "." + price_parts[1] + "";
            document.getElementById(price_id).innerHTML = formatted_price;
        }
    
    }
    

    And here's the CSS I used:

    .rel-prod-item p.rel-prod-price b {
        font-size: 50%;
        position: relative;
        top: -4px;
    }
    

    I hope this helps someone keep all their hair :-)

    Here's a screenshot of the finished product

提交回复
热议问题