将jquery侦听器转换为javascript侦听器

时间:2012-07-20 12:38:21

标签: javascript jquery keylistener

我有这样的代码:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">
  $(document).ready(function(){
      var my_id = 'iddate';

      $('#'+ my_id).bind('keyup',function(e){ // Listener for key pressing in the field.
          var inputValue = document.getElementById( my_id ).value;
          // And etc...
      })
  });
</script>

这里我使用了JQuery。但问题是我不能再使用它了。可以用javascript重写它吗?

2 个答案:

答案 0 :(得分:1)

是的,你可以,例如:

window.onload = function() {
  var my_id = 'iddate';

  document.getElementById( my_id ).onkeyup = function(e){ // Listener for key pressing in the field.
      var inputValue = document.getElementById( my_id ).value;
      // And etc...
  };
}

或者使用addEventListener

document.getElementById( my_id ).addEventListener('click', function(e){ // Listener for key pressing in the field.
      var inputValue = document.getElementById( my_id ).value;
      // And etc...
}, false); 

答案 1 :(得分:0)

或者你也可以这样做,

window.onload = function(){
   var my_id = 'iddate';
      document.getElementById( my_id ).onkeyup = function(e){ // Listener for key pressing in the field.
      var inputValue = document.getElementById( my_id ).value;
     };
}