改进:更改mousedown上的文本/背景按钮-revert on mouseup
Improve This: Change button text/background onmousedown - revert onmouseup
我想看看社区中是否有人可以改进这段代码。
目标:应用程序充满了输入元素,这些元素的样式看起来像自定义按钮。它们有各种类型,例如"提交"、"重置"answers"按钮"。当用户点击按钮(即用鼠标在PC上点击按钮,或触摸触摸屏设备(如BlackBerry)上正确位置的屏幕)时,按钮文本和背景应发生变化,以指示按钮已被按下。在执行与按钮相关的操作之前,文本和背景应该恢复,以指示按钮已被释放。
以下是我为我的解决方案准备的代码片段——有人能找到改进/slim/rebuild的方法吗?
<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的方法。如果您希望按下的外观是以下CSS
.pressed { color : #ffffff; background-color : #FEC800; }
那么你的功能很简单:
function RegisterJQueryFunctions() {
$('input[type=submit], input[type=button], input[type=reset]')
.mousedown(function () { $(this).toggleClass("pressed",true); })
.mouseup(function () { $(this).toggleClass("pressed",false); });
}
您可以为不同的输入类型(使用标准的CSS选择器)提供不同的按压外观。
您正在将样式与代码分离(这总是一件好事。)
使用CSS(class),您可以大幅减少代码的大小。其次,CSS将提供在不更改javascript(主题)的情况下更改颜色的选项。CSS的唯一目的是给网站的外观和感觉&对于javascript,向站点添加动态行为。
希望这能有所帮助。
- 如果多次使用相同的jQuery选择器,则应该缓存返回并重用第一个jQuery对象
- 如果在同一个jQuery对象上调用多个jQuery方法,则可以将它们链接起来
- 我不喜欢一次性变量:如果一个变量只被分配了一个值放置和该值仅在一个(另一个)位置使用,则不需要它
- 如果使用
.data()
(或.attr()
或其他几个jQuery方法)设置多个值,则可以通过将属性放入映射中,通过一个调用完成所有操作
因此:
function RegisterJQueryFunctions() {
$('input[type=submit], input[type=button], input[type=reset]').mouseup(function () {
$(this).revertFont().revertBG();
})
.mousedown(function () {
// Record the original font and BG colors so we can revert back to them in 'delight'
var $this = $(this);
$this.data({"originalFont" : $this.css("color"),
"originalBG" :, $this.css("background-color")
})
.highlightBG()
.highlightFont();
});
$.fn.highlightFont = function (highlightFontColor) {
$(this).css("color", highlightFontColor || "#FFFFFF");// Defaults to white
};
$.fn.highlightBG = function (highlightBGColor) {
$(this).css("background-color", highlightBGColor || "#FF7F00";);
};
$.fn.revertFont = function () {
var $this = $(this);
// Defaults to black in case data attribute wasn't set properly in highlightFont
$this.css("color", $this.data("originalFont") || "#000000");
};
$.fn.revertBG = function () {
var $this = $(this);
// Defaults to orange in case data attribute wasn't set properly in highlightFont
$this.css("background-color", $this.data("originalBG") || "#FEC800");
};
}
使用以上建议的组合,这就是最终的解决方案:
$('input[type=submit], input[type=button], input[type=reset]')
.mousedown(function () { $(this).toggleClass("pressed", true); })
.mouseup(function () { $(this).toggleClass("pressed", false); })
.focusout(function () { $(this).toggleClass("pressed",false); });
$('a,a>*')
.mousedown(function () { $(this).toggleClass("pressed",true); })
.mouseup(function () { $(this).toggleClass("pressed", false); })
.focusout(function () {
$(this).toggleClass("pressed", false);
$(this).children().toggleClass("pressed", false); // call explictily because some elements don't raise the 'focusout' event
});
相关文章:
- 蓝色和透明的按钮背景
- 谷歌工作表中的按钮,当单击时,会更改特定单元格的背景
- "隐藏/显示”;元素和更改按钮背景图像
- 如何在 Firefox 扩展的 JavaScript 中的按钮背景中插入图像
- 如何使用jquery在鼠标悬停时更改按钮背景颜色
- CSS 按钮背景图像在 IE9 中不起作用
- HTML - 如何在 OnClick 事件上更改按钮背景颜色
- ExtJS基于颜色选择器动态更改按钮背景颜色
- 按钮背景颜色不起作用
- 按钮背景颜色更改
- 按钮背景颜色切换
- Button .js中的按钮背景颜色
- 我如何用javascript改变一个按钮背景
- 按钮背景更改点击
- 有没有办法用颜色填充一半的按钮背景?
- 为什么我的按钮背景没有改变颜色?
- 如何在extjs中删除按钮背景
- CSS3将按钮背景颜色从左向右更改
- jQuery onClick 单选按钮背景更改
- 单击时切换按钮背景颜色