如何为 HTML 中的多个元素设置相同的 ID
how to set the same id for multiple element in html
我想对运行时创建的 10 个元素使用倒数计时器。 每个元素都有过期时间,所以我想向用户显示过期的时间是多少 remained.so 我使用 jquery 文件来执行此操作,所以我必须使用一个 id 作为标签来显示剩余时间.当我将其用于一个元素时,它可以正常工作,但是当我将其用于多个元素时,它仅适用于第一个元素,我如何解决此问题以显示所有元素的剩余时间
J查询文件
//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 中使用类是唯一的。 更多元素可以具有相同的类
$('.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 页面中使用一次。此外,和元素应该只有一个 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";
这是类和 id 的一个很好的参考。
相关文章:
- 使用 JavaScript 一次为一个元素设置多个属性
- 为dropdownlistfor(MVC)中的每个元素设置唯一id
- 无法为HTML5中具有画布和图像的可拖动元素设置“拖动图像”
- Jquery为每个元素设置属性
- 在JSON数组中进行迭代,并为其元素设置样式
- 将jquery ui窗口的父元素设置为另一个元素
- 如何使用随机css id为元素设置样式
- 如何为所有子元素设置填充
- Javascript:为元素设置适当的事件处理程序
- 为已设置动画的元素设置动画
- 将HTML元素设置为可滚动
- jQuery:从 JSON 对象为元素设置值
- 如何为 HTML 中的多个元素设置相同的 ID
- 为动态创建的元素设置属性
- 同时为一个 Li 元素设置类“活动”
- 如何使用 javascript 为元素设置内联 css 样式
- 如何为动态添加的 html 元素设置 setCustomValidity()
- 为包含 HTML 标记的字符串中的元素设置 Style 属性
- 获取两个元素的高度,将另一个元素设置为该高度
- 为表单元素设置 Cookie