简体   繁体   English

将“值”从滑块传递给元素,作为属性的参数

[英]Passing "value" from slider to an element, as an parameter of an attribute

I'm trying to capture an HTML slider value , which is suppose to set a parameter of an attribute (inside a CSS class).我正在尝试捕获一个HTML slider value ,它假设设置一个属性的参数(在CSS类中)。

So, i have a slider and a div element - to which i want to pass a value to:所以,我有一个slider和一个div元素 - 我想将一个值传递给:

<input type="range" min="1" max="11" value="6" class="slider" id="slider">

<div id="container">
     <div id="fg2">text</div>
</div>

In JS I've set those two elements as variables, and added Event Listener to the slider , with a function that captures value from the slider :在 JS 中,我将这两个元素设置为变量,并将Event Listener添加到slider ,并使用一个从slider捕获value的函数:

const slider = document.getElementById("slider")
const FGElement = document.getElementById("fg2");

slider.addEventListener("input", function(){
    changeSize();
});

function changeSize() {
    let i = slider.valueAsNumber;
    console.log(i);
}

Now, i want to add a class to FGElement , inside of which i would put flex-grow: [i] .现在,我想向FGElement添加一个类,在其中放置flex-grow: [i] Which is my idea to how dynamically capture value from slider to FGElement .这是我如何从slider动态捕获valueFGElement

Is it the right way to do it, if so - how to set an parameter of an CSS class attribute in JS?如果是这样,这是正确的方法吗 - 如何在 JS 中设置CSS class attribute的参数?

If not, what would be the correct way...如果没有,正确的方法是什么...

You can't dynamically change values of .css file您不能动态更改.css文件的值

You have to Use JS for to apply inline style你必须使用 JS 来应用内联样式

document.getElementById('myElement').style.flexGrow = i;

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

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