【问题标题】:Call js function when button is pressed按下按钮时调用js函数
【发布时间】:2015-09-07 06:00:44
【问题描述】:

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

js代码:

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

【问题讨论】:

  • 我认为,这很容易。它应该是点击而不是按键。
  • 那么按键是干什么用的?好吧,对于键盘键...

标签: javascript jquery html events keypress


【解决方案1】:

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

Demo

jQuery(document).ready(function() {
  $('#ref1').click(function() {
    alert('Handler for .click() called.');
  });
});
<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>

【讨论】:

    猜你喜欢
    • 2013-02-11
    • 2020-03-30
    • 1970-01-01
    • 1970-01-01
    • 2013-08-21
    • 2013-11-06
    • 2019-08-26
    相关资源
    最近更新 更多