简体   繁体   English

键盘上的jQuery触发css

[英]jquery on keyup trigger css

I'm having trouble getting this short script to work. 我很难让这个简短的脚本正常工作。 I want to set the width and height of a div by taking the values of input elements. 我想通过输入元素的值来设置divwidthheight

<input id="width" type="number" class="num" />
<input id="height" type="number" class="num" />
<br />
<br />
<div>
</div>

var w = +$('#width').val();
var h = +$('#height').val();

var width = w * 10;
var height = h * 10;

$('.num').keyup(function(){
$('div').css({
    'width' : width,
    'height' : height
});
});

JSFIDDLE 的jsfiddle

How can I get this to work? 我该如何工作?

Here's a quick version you can start working with that only adjusts the width. 这是您可以开始使用的快速版本,仅可调整宽度。 Once you get that working, then add height. 一旦完成工作,然后增加高度。

$('#width').keyup(function(){
  var w = $(this).val() * 10;
  $('div').css('width',w);
});

Updated fiddle: http://jsfiddle.net/djd0jqo1/12/ 更新的小提琴: http : //jsfiddle.net/djd0jqo1/12/

$('.num').keyup(function(){
  var w = +$('#width').val();
  var h = +$('#height').val();
  var width = w * 10;
  var height = h * 10;
  $('div').css({
    'width' : width,
    'height' : height
  });
});

You need to get the val the moment the person changes, they way you coded, it gets the value of width and height the moment the script is run, and it doesnt ever change. 您需要在人员更改时获取val,以他们的编码方式获取值,在脚本运行时获取width和height的值,并且此值永远不会更改。

$('.num').keyup(function(e){
 var w = +$('#width').val();
 var h = +$('#height').val();
 var width = w * 10;
 var height = h * 10;
if(width!=0)
{
$('div').css(
    'width' , width+"px"

)
}
  if(height!=0)
 {
  $('div').css('height',height+'px');
 }
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM