问题
Using a script that reference jQuery Mobile I have the following line:
<a href="#" id="buttonAnswer1" data-role="button" data-inline="true" >check</a>
How would I add a listener for when this button is clicked and lets say call the hello() function? i.e.
<a href="#" id="buttonAnswer1" data-role="button" data-inline="true">check</a>
<script>
function hello(){
console.log("hello world");
}
</script>
回答1:
This isn't really any different then regular jQuery, that is you can use .on to bind an event handler
For example, using .on with event delegation
$(document).on('click', '#buttonAnwser1', hello);
Or you can bind it directly
$('#buttonAnswer1').on('click', hello);
来源:https://stackoverflow.com/questions/13794480/using-jquery-mobile-button-to-run-javascript