[英]How do I limit .the keydown JavaScript event?
This is the file http://www.frysa.us/Arctext/Size.html这是文件http://www.frysa.us/Arctext/Size.html
I tried to limit the font size using an if statement:我尝试使用 if 语句来限制字体大小:
<script>
$(function(){
var KEY_UP = 38;
var KEY_DOWN = 40;
//Set listener for keydown
$(document).keydown(function(e){
$("anything").html("- keydown -");
switch(e.which){
case KEY_UP:
var fontsize = parseInt($("#example4").css("font-size")) + 2;
$("#example4").css("font-size", fontsize+"px");
if (("#example4").css("font-size")<20){
alert("it is less than 20")
//some sort of code to limit the font-size
}
break;
case KEY_DOWN:
var fontsize = parseInt($("#example4").css("font-size")) - 2;
$("#example4").css("font-size", fontsize+"px");
break;
}
});
});
</script>
Not much luck.. Can someone point me in the right direction运气不太好..有人能指出我正确的方向吗
You just have to convert the CSS string to an integer.您只需将 CSS 字符串转换为整数即可。 Try this using replace :
使用replace试试这个:
var fontSize = $("#example4").css("font-size");
var intFontSize = parseInt(fontSize.replace('px',''));
if (intFontSize<20){
alert("it is less than 20")
//some sort of code to limit the font-size
}
(also, note that you have a typo, this line is missing the $ ): (另请注意,您有错字,此行缺少$ ):
if (("#example4").css("font-size")<20){
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.