我想看看社區中的任何人是否可以改善此代碼。改善此行爲:更改按鈕文字/背景onmousedown - 恢復onmouseup
目標:應用程序中充滿了輸入元素,它們的樣式看起來像自定義按鈕。它們有各種類型,例如「提交」,「重置」和「按鈕」。當用戶點擊按鈕時(即用鼠標在PC上點擊它或在觸摸屏設備上的正確位置觸摸屏幕,例如黑莓),按鈕文本&背景應該改變以指示按鈕已被按下。文本&背景應該在與按鈕相關聯的動作被執行之前恢復 - 指示按鈕已被釋放。
這是我的解決方案的代碼片段 - 任何人都可以看到改進/ slim/refactor的方法嗎?
<script type="text/javascript">
$(document).ready(function() {
RegisterJQueryFunctions();
});
</script>
在一個外部文件:
function RegisterJQueryFunctions() {
$('input[type=submit], input[type=button], input[type=reset]').mousedown(function() {
// Record the original font and BG colors so we can revert back to them in 'delight'
var originalFont = $(this).css("color");
var originalBG = $(this).css("background-color");
$(this).data("originalFont", originalFont);
$(this).data("originalBG", originalBG);
$(this).highlightBG();
$(this).highlightFont();
});
$('input[type=submit], input[type=button], input[type=reset]').mouseup(function() {
$(this).revertFont();
$(this).revertBG();
});
$.fn.highlightFont = function (highlightFontColor) {
var highlightFont = highlightFontColor || "#FFFFFF"; // Defaults to white
$(this).css("color", highlightFont);
};
$.fn.highlightBG = function (highlightBGColor) {
var highlightBg = highlightBGColor || "#FF7F00"; // Defaults to orange
$(this).css("background-color", highlightBg);
};
$.fn.revertFont = function() {
var originalFont = $(this).data("originalFont");
if (!originalFont.length)
originalFont = "#000000"; // Defaults to black in case data attribute wasn't set properly in highlightFont
$(this)
.css("color", originalFont);
};
$.fn.revertBG = function() {
var originalBG = $(this).data("originalBG");
if (!originalBG.length)
originalBG = "#FEC800"; // Defaults to orange in case data attribute wasn't set properly in highlightFont
$(this)
.css("background-color", originalBG);
};
}
這通常是與CSS樣式。 – Hogan 2012-01-05 05:10:00