簡體   English   中英

如何在html中為多個元素設置相同的id

[英]how to set the same id for multiple element in html

我想對運行時創建的10個元素使用倒數計時器。 每個元素都有過期時間,因此我想向用戶顯示剩余的過期時間。因此,我使用jquery文件執行此操作。因此,必須使用id作為標記來顯示剩余時間。對於一個元素它工作正常,但是當我將其用於多個元素時它僅適用於第一個元素。我如何解決這個問題以顯示所有元素的剩余時間

jQuery文件

    //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在html use類中是唯一的,而不是.. more元素可以具有相同的類

$('.yourClass') 

代替

$('#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";
}

編輯:我為此創建了一個JSFiddle,您可以精確化您的請求嗎?

在這里看到

我改變了這個:

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

而這個功能:

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

ID是唯一的,並且只能在html頁面中使用一次。 另外,and元素應該只有一個ID。 不是唯一的,因此多個元素可以具有相同的類,而且單個元素可以具有多個類。 例:

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

代替id="timer"使用class="timer"然后在您的javascript文件中使用$(".timer")定位這些類。 因此,在您的情況下, this.target_id = "#timer"使用this.target_id =".timer";而不是this.target_id = "#timer" this.target_id =".timer";

這是有關class和id的很好的參考。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM