【问题标题】:Call js function when button is pressed按下按钮时调用js函数
【发布时间】:2015-09-07 06:00:44
【问题描述】:
我想在按下按钮时调用 js 函数。在阅读了this 和this 之后,我在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>