当用户按退格键 (8) 或回车键 (13) 时,我不会运行此函数。
$(document).keypress(function(e) {
if(e.which == 8) { //-- or 13
var x = document.getElementsByTagName("BODY")[0];
x.style.backgroundImage = "-moz-linear-gradient(--90deg, #004158 0%, #005472 100%)";
x.style.backgroundImage = "-webkit-linear-gradient(--90deg, #004158 0%, #005472 100%)";
x.style.backgroundImage = "-o-linear-gradient(--90deg, #004158 0%, #005472 100%)";
x.style.backgroundImage = "linear-gradient(-180deg, #004158 0%, #005472 100%)";
}
});
但是这段代码不起作用。
谢谢。
最佳答案
你使用什么版本的 jQuery?
$(document).on('keypress', function(e) {
if(e.which == 8 || e.which == 13) { //-- or 13
var x = document.getElementsByTagName("BODY")[0];
x.style.backgroundImage = "-moz-linear-gradient(--90deg, #004158 0%, #005472 100%)";
x.style.backgroundImage = "-webkit-linear-gradient(--90deg, #004158 0%, #005472 100%)";
x.style.backgroundImage = "-o-linear-gradient(--90deg, #004158 0%, #005472 100%)";
x.style.backgroundImage = "linear-gradient(-180deg, #004158 0%, #005472 100%)";
e.preventDefault();
}
});
关于javascript - 按下键并运行 jquery 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31228786/