简体   繁体   English

如何使用HTML输入从JavaScript动态更改CSS类属性

[英]How to change css class property dynamically from javascript using html input

 <script type="text/javascript"> function calculate() { var myBox1 = document.getElementById('box1').value; var myBox2 = document.getElementById('box2').value; if (showAlert(myBox1, 'Width') && showAlert(myBox2, 'Height')) { var result = document.getElementById('result'); var myResult = [(myBox1 * myBox2 * 0.69)/100]; result.value = parseFloat(myResult).toFixed(2); } } </script> 
 .cropper-face, .cropper-line, .cropper-point { position: fixed; display: block; width: 100%; height: 100%; filter: alpha(opacity=10); opacity: .1; } 
 <input id="box1" type="text" onchange="calculate()"/> <input id="box2" type="text" onchange="calculate()"/> <input id="result" type="text" readonly="readonly" onchange="calculate()"/> 

I want to change width and height property dynamically using html input field. 我想使用html输入字段动态更改width和height属性。

Can anyone tell me how can this possible by using this input as css property width and height in this code 谁能告诉我如何通过使用此输入作为此代码中css属性的宽度和高度来做到这一点

I have two text-box for width & height. 我有两个用于宽度和高度的文本框。 and it should be change width and height of this css when I enter values in this field. 当我在此字段中输入值时,应该更改此CSS的宽度和高度。

As there are 3 classes which has same property. 由于存在3个具有相同属性的类。 How can I change its width and height according to user input . 如何根据用户输入更改其宽度和高度。

Thanks in advance. 提前致谢。

To change the element dimensions dynamically when input value changes, you'll have to do the following: 要在输入值更改时动态更改元素尺寸,您必须执行以下操作:

var widthInput = $('#width');
var heightInput = $('#height');
var targetElement = $('#target');

set the values as the element dimensions 将值设置为元素尺寸

var onInputChange = function(){
    //getting the values from the width and height inputs
    var width = widthInput.val() || 0;
    var height = heightInput.val() || 0;
    //setting the values we got as the width and height
    //for the element
    targetElement.css({
        width: width + 'px',
        height: height + 'px'
    });
};

listen to keyup events on the inputs like so 像这样听输入的键盘事件

//listening for keyup events for both width and height inputs
//NOTE: I used 'keyup' event and not 'change' like your example
widthInput.keyup(onInputChange);
heightInput.keyup(onInputChange);

Here is a working example using jQuery: http://jsfiddle.net/6cjsLdcj/1/ 这是一个使用jQuery的工作示例: http : //jsfiddle.net/6cjsLdcj/1/

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

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