如何在 jQuery 中使用滑块更改样式值
How to change value of style using a slider in jQuery
我有一个滑块可以在 jQuery 中更改其值。我如何使用此滑块更改宽度中样式的值,以便 div 的宽度与滑块的值相对应。我已将 css 设置为将宽度更改为滑块的值,但它不起作用。
我有这样的代码:
<div class="slidecontainer">
<input type="range" min="1" max="100" value="50" class="slider" id="myRange">
<p>Value: <span id="demo"></span></p>
</div>
<div id="changecss" style="width: 50%;">Change WIDTH</div>
和jQUery
var slider = document.getElementById("myRange");
var output = document.getElementById("demo");
var changecss = document.getElementById("changecss");
var a;
output.innerHTML = slider.value;
slider.oninput = function() {
output.innerHTML = this.value;
a = this.value;
$("#changecss").css("width", a);
}
请参阅下面的代码。
<div class="slidecontainer">
<input type="range" min="1" max="100" value="50" class="slider" id="myRange">
<p>Value: <span id="demo"></span></p>
</div>
<div id="changecss" style="width: 50%;background-color:blue;">Change WIDTH</div>
<script
src="https://code.jquery.com/jquery-3.6.0.min.js"
integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4="
crossorigin="anonymous"></script>
<script>
var slider = document.getElementById("myRange");
var output = document.getElementById("demo");
var changecss = document.getElementById("changecss");
var a;
output.innerHTML = slider.value;
slider.oninput = function() {
output.innerHTML = this.value;
a = this.value;
$("#changecss").css("width", a+"%");
}
</script>
我有一个滑块可以在 jQuery 中更改其值。我如何使用此滑块更改宽度中样式的值,以便 div 的宽度与滑块的值相对应。我已将 css 设置为将宽度更改为滑块的值,但它不起作用。
我有这样的代码:
<div class="slidecontainer">
<input type="range" min="1" max="100" value="50" class="slider" id="myRange">
<p>Value: <span id="demo"></span></p>
</div>
<div id="changecss" style="width: 50%;">Change WIDTH</div>
和jQUery
var slider = document.getElementById("myRange");
var output = document.getElementById("demo");
var changecss = document.getElementById("changecss");
var a;
output.innerHTML = slider.value;
slider.oninput = function() {
output.innerHTML = this.value;
a = this.value;
$("#changecss").css("width", a);
}
请参阅下面的代码。
<div class="slidecontainer">
<input type="range" min="1" max="100" value="50" class="slider" id="myRange">
<p>Value: <span id="demo"></span></p>
</div>
<div id="changecss" style="width: 50%;background-color:blue;">Change WIDTH</div>
<script
src="https://code.jquery.com/jquery-3.6.0.min.js"
integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4="
crossorigin="anonymous"></script>
<script>
var slider = document.getElementById("myRange");
var output = document.getElementById("demo");
var changecss = document.getElementById("changecss");
var a;
output.innerHTML = slider.value;
slider.oninput = function() {
output.innerHTML = this.value;
a = this.value;
$("#changecss").css("width", a+"%");
}
</script>