简体   繁体   中英

how to set the same id for multiple element in html

I want to use countdown timer for 10 element that creating at run time. each element has expire time so I want to show user how much time of of the expiration is remained.so I use a jquery file to do this .so I must use an id for a tag to show the remained time .when I use it for one element it works fine but when I use it for multiple element it just works for first element.how can I solve this problem to show the remained time for all elements

Jquery file

    //var count = 1000;
    //var counter = setInterval(timer, 1000);
    //function timer() {
    //    count -= 1;
    //    if (count==1000) {
    //        clearInterval(counter);
    //    }
    //    document.getElementById("num").innerHTML = count;
    //}
    function CountDown() {

        this.start_time = "02:00:00:23";
        this.target_id = "#timer";
        this.name = "timer";
    }

CountDown.prototype.init=function(){
    this.reset();
    setInterval(this.name+'.tick()',1000);
}
CountDown.prototype.reset=function(){
    time = this.start_time.split(":");
    this.days = parseInt(time[0]);
    this.hours = parseInt(time[1]);
    this.minutes=parseInt(time[2]);
    this.seconds = parseInt(time[3]);
    this.update_target();
}

CountDown.prototype.tick=function(){
    if (this.seconds > 0 || this.minutes > 0 || this.hours > 0 ||this.days>0) {

        if (this.hours == 0 && this.minutes == 0 && this.seconds == 0) {
            this.days = this.days - 1;
            this.hours = 23;
            this.minutes = 59;
            this.seconds = 59;
        }
        if (this.minutes == 0 && this.seconds==0) {
            this.hours = this.hours - 1;
            this.minutes = 59;
            this.seconds = 59;
        }
        else if (this.seconds == 0) {
            this.minutes = this.minutes - 1;
            this.seconds = 59;
        }
        else {
            this.seconds = this.seconds - 1;
        }

    }
    this.update_target();
}

CountDown.prototype.update_target = function () {
    seconds = this.seconds;
    minutes = this.minutes;
    hours = this.hours;
    days = this.days;
    if (seconds<10) 
        seconds = "0"+seconds;
    if (minutes < 10)
        minutes = "0"+ minutes;
    if (hours < 10)
        hours = "0" + hours;
    if (days < 10)
        days = "0" + days;
    $(this.target_id).val(days+":"+hours+":"+minutes + ":" + seconds)
   // $(this.target_id).val(this.minutes+":"+seconds) 


}

 Html




    <script src="~/Scripts/jquery-1.4.4.min.js"></script>
    <script src="~/Scripts/countdown.js"></script>
    <input type="text" id="timer" value=" " />
    <script>
        timer = new CountDown();
        timer.init();
    </script>

Id is unique in html use class instead .. more element can have the same class

$('.yourClass') 

instead of

$('#yourId') 

.

 <script src="~/Scripts/jquery-1.4.4.min.js"></script>
  <script src="~/Scripts/countdown.js"></script>
  <input type="text"  class="timer" value=" " />
  <script>
    timer = new CountDown();
    timer.init();
  </script>


function CountDown() {

    this.start_time = "02:00:00:23";
    this.target_id = ".timer";
    this.name = "timer";
}

EDIT : I've created a JSFiddle for it, can you precise your request ?

See it here

I changed this :

timer = new CountDown("02:00:00:23");
timer.init();   

And this function :

function CountDown(start_time) {
  this.start_time = start_time;
  this.target_id = ".timer";
  this.name = "timer";
}

Id's are unique and should only be used once in an html page. Also, and element should only have a single ID. Classes are not unique so multiple elements can have the same class, also, a single element can have multiple classes. Example:

<div class="exampleClass anotherClass"></div>
<div class="exampleClass></div>
<div class="exampleClass></div>

Instead of id="timer" use class="timer" then in your javascript file use $(".timer") to target those classes. So in your case instead of this.target_id = "#timer" use this.target_id =".timer";

Here's a good reference for classes and ids .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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