如何用html组件绑定一个函数?

时间:2013-01-11 07:47:49

标签: javascript html html5 web components

这是我的设置。我创建一个html页面并在此页面上放置一个输入组件,其类型为text。我为组件设置了readonly属性,因为我想在此组件中显示日期和时间,因此用户无法更改它。我的问题是如何在此组件中显示日期和时间,以便其值随时间变化而不断变化。感谢

3 个答案:

答案 0 :(得分:1)

如果您使用的是html组件ID

,请尝试此操作
  $("#main").bind("click",function(){
      alert("The is id.");

//or you can call the function 
    });

或者 如果您使用的是html组件类

,请尝试这样做
 $(".main").bind("click",function(){
      alert("The is class.");
//or you can call the function 
    });

答案 1 :(得分:0)

使用jquery代替

HTML

<label id="clock"></label>

jquery的

var updateClock = function() {
function pad(n) {
    return (n < 10) ? '0' + n : n;
}

var now = new Date();
var s = pad(now.getUTCHours()) + ':' +
        pad(now.getUTCMinutes()) + ':' +
        pad(now.getUTCSeconds());

 $('#clock').html(s);

var delay = 1000 - (now % 1000);
setTimeout(updateClock, delay);

};

答案 2 :(得分:0)

在每个日期时间更新中运行此代码

 document.getElementById("input_id").value = "date content";