【问题标题】:How to bind a function with html component?如何将函数与 html 组件绑定?
【发布时间】:2012-12-25 18:41:20
【问题描述】:

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

【问题讨论】:

    标签: javascript html web components


    【解决方案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 
        });
    

    【讨论】:

      【解决方案2】:

      为此使用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);
      
      };
      

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

        • 请检查答案的最后一部分。
        • document.getElementsByTag("input").value === reference error
        猜你喜欢
        • 2016-08-18
        • 1970-01-01
        • 2018-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-02
        • 2018-05-01
        • 2019-04-12
        相关资源
        最近更新 更多