使其<button>
具有绑定到它的onclick
事件,该事件执行 AJAX 请求以将请求提交给将更新数据库或类似的页面处理程序,并在成功完成时更改其 CSS 样式以使其看起来被按下。
使用 jQuery 进行事件绑定、AJAX 请求和CSS 类更改,这种样式应该可以使按钮看起来很压抑。
<button type="button" id="btnPlay">Play</button>
然后:
$("#btnPlay").click(function(){
var button = $(this) ;
if (!button.hasClass('pressed')) {
//button is unpressed
$.post("ajax.php", { userId: "5512", action: "play" },
function(data){
button.addClass('pressed');
button.html("Playing!") ;
}
);
}
else {
//button is pressed, toggle it back
$.post("ajax.php", { userId: "5512", action: "stopPlaying" },
function(data){
button.removeClass('pressed');
button.html("Play") ;
}
);
}
});