按下按钮时调用js函数

时间:2015-09-07 06:00:44

标签: javascript jquery html events keypress

我想在按下按钮时调用js函数。阅读thisthis后,我在jsFiddle中做了类似的事情。但是,当我按下按钮时,没有任何反应!我错过了什么?

js code:

jQuery(document).ready(function() {
    $('#ref1').keypress(function() {
        alert('Handler for .keypress() called.');
    });
});

1 个答案:

答案 0 :(得分:1)

keypress元素上不能有<a>个事件,请使用click

Demo

&#13;
&#13;
jQuery(document).ready(function() {
  $('#ref1').click(function() {
    alert('Handler for .click() called.');
  });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<div data-role="page" id="pageone">
  <div data-role="main" class="ui-content">
    <a href="#" id="ref1" title="Refresh page if you did shit" data-role="button" data-icon="refresh" data-iconpos="notext" data-theme="b" data-inline="true" class="ui-link ui-btn ui-btn-b ui-icon-refresh ui-btn-icon-notext ui-btn-inline ui-shadow ui-corner-all"
    role="button">
      <p>Refresh page (helpful in case of mistake)</p>
    </a>

  </div>
</div>
&#13;
&#13;
&#13;