JS不会仅读取第一个ID

时间:2016-08-03 12:52:05

标签: javascript html

好的我有一些JS,取决于进度条加载的百分比,并在栏内显示%。

见下面的工作代码

$("#next-button").click(function() {
        a.eventHub.publish("ui/nextclick")
      });

    UI.prototype.updatePercentile = function(a) {
        if (this.isStandalone || -1 == a.percentile) $("#results-percentages")
          .hide();
        else {
          $("#results-percentages")
            .show(), $("#results-percentile-1")
            .html(a.percentile), $("#results-suffix")
            .html(a.suffix), $("#results-suffix-2")
            .html(a.suffix), 1 === a.timesTaken ? ($("#results-timestaken")
              .html(a.timesTaken + " time"), $("#results-percentile-2")
              .html("100")) : ($("#results-timestaken")
              .html(a.timesTaken + " times"), $("#results-percentile-2")
              .html(a.percentile));
          var b = 0;
          $("#percentile-scale .scale-item")
            .each(function() {
              b += $(this)
                .outerWidth()
            }), $("#percentile-scale .scale-indicator").css("width", b * a.percentile / 100 + "%"),
            $(".scale-marker").css("left", b * a.percentile / 100 + "%")
        }
    }

        function move() {
          var elem = document.getElementById("myBar");
          var width = 0;
          var id = setInterval(frame, 50);

          function frame() {
            if (width >= document.getElementById("results-percentile-2").innerHTML) {
              clearInterval(id);
            } else {
              width++;
              elem.style.width = width + '%';
              document.getElementById("demo").innerHTML = width * 1 + '%';
            }
          }
        }

然而现在我想引入一个新的ID来添加百分比,它会显示为10th3rd,具体取决于分数,但是当我改变我的JS以包括其他ID(见下文)它只显示th,rd或st no数字。

HTML

   <h3 style="text-align: left;"> You scored in the <em class="big-six"><span id="results-percentile-1"></span><span id="results-suffix-2"></span></em> percentile.
                        </h3>

    <div class="myProgress">
                            <div id="myBar" style="width:0">
                                <div id="demo"><span id="results-suffix"></span></div>
                                <!--<div id="demo"></div>-->
                            </div>

JS

(function() {
        var elem = document.getElementById("myBar");
        var width = 0;
        var id = setInterval(frame, 50);

        function frame() {
            if (width >= Number(document.getElementById("results-percentile-2").innerHTML)) {
                clearInterval(id);
            } else {
                width++;
                elem.style.width = width + '%';
                document.getElementById("demo").innerHTML = width * 1 + document.getElementById("results-suffix").innerHTML   ;
            }
        }
    }());

1 个答案:

答案 0 :(得分:1)

ID必须是唯一的。如果您需要具有相同标识符的多个元素,请使用类。

例如:

<div id="myBar" style="width:0">
    <div class="demo"><span class="results-suffix"></span></div>
    <div class="demo"></div>
</div>

您的javascript将更改为:

function frame() {
    if (width >= Number(document.getElementById("results-percentile-2").innerHTML)) {
        clearInterval(id);
    } else {
        width++;
        elem.style.width = width + '%';
        var demoElems = document.getElementsByClassName('demo');

        var i, innerHTML, resultsElems;
        // loop through the list of elements
        for(i = 0; i < demoElems.length; i++) {
            resultsElems = demoElems[i].getElementsByClassName('results-suffix')
            innerHTML = width * 1;
            // only append the results-suffix element if it exists
            if(resultsElems.length > 0) {
                innerHTML += resultsElems[0].innerHTML;
            }
            demoElems[i].innerHTML = innerHTML;
        }
    }
}