开发者问题收集

同一页面上的两个滑块值

2020-02-14
50

我试图将两个不同的滑块放在同一个页面上。如果我只对一个滑块执行下面的操作,它就可以正常工作:

<h3>Strength of Belief</h3>
<div class="slidecontainer">
<div class="slider_left">
    <span id="belief_value"></span>%
</div>
<div class="slider_mid">
    <input type="range" min="1" max="100" value="50" class="slider" id="oldbelief">
</div>
</div>

<script>
var slider = document.getElementById("oldbelief");
var output = document.getElementById("belief_value");
output.innerHTML = slider.value;

slider.oninput = function() {
  output.innerHTML = this.value;
}
</script>

但是,如果我尝试使用下面的代码添加第二个滑块,则第二个滑块上的值显示不起作用:

<h3>Strength of Belief</h3>
<div class="slidecontainer">
<div class="slider_left">
    <span id="belief_value"></span>%
</div>
<div class="slider_mid">
    <input type="range" min="1" max="100" value="50" class="slider" id="oldbelief">
</div>
</div>

<h3><i>New</i> Strength of Belief in Negative Cognition</h3>
<div class="slidecontainer">
<div class="slider_left">
    <span id="belief2_value"></span>%
</div>
<div class="slider_mid">
    <input type="range" min="1" max="100" value="50" class="slider2" id="newbelief">
</div>
</div>

<script>
var slider = document.getElementById("oldbelief");
var output = document.getElementById("belief_value");
output.innerHTML = slider.value;

slider.oninput = function() {
  output.innerHTML = this.value;
}
var slider2 = document.getElementById("newbelief");
var output2 = document.getElementById("belief2_value");
output.innerHTML = slider.value;

slider2.oninput = function() {
  output2.innerHTML = this.value;
}
</script>

如果重要,请在此处查看 CSS:

.slider_left {
    display: inline-block;
    width: 10%;
    text-align: center;
    font-weight: normal;
    font-size: 30px;
    color: #fff;
}

.slider_mid {
    display: inline-block;
    width: 79%;
}
.slidecontainer {
  width: 100%;
}

.slider, .slider2 {
  -webkit-appearance: none;
  width: 100%;
  height: 10px;
  border-radius: 5px;
  background: #999;
  outline: none;
  opacity: 1;
  -webkit-transition: .2s;
  transition: opacity .2s;
}

有人知道我做错了什么吗?我是 javascript 新手,所以我的错误可能非常令人尴尬。

2个回答

您在此处犯了一个错误 output.innerHTML = slider.value; 。您应该将目标定位到 output2 ,因为您为第二个滑块创建了它。

并且您在 CSS 上的 .slider_left 类设置了颜色: #fff 这就是为什么这是文本将与背景一样为白色并且不可见的另一个原因

您可以在下面看到它应该是怎样的

var slider = document.getElementById("oldbelief");
var output = document.getElementById("belief_value");
output.innerHTML = slider.value;

slider.oninput = function() {
  output.innerHTML = this.value;
}
var slider2 = document.getElementById("newbelief");
var output2 = document.getElementById("belief2_value");
output2.innerHTML = slider.value;

slider2.oninput = function() {
  output2.innerHTML = this.value;
}
.slider_left {
    display: inline-block;
    width: 10%;
    text-align: center;
    font-weight: normal;
    font-size: 30px;
}


.slider_mid {
    display: inline-block;
    width: 79%;
}
.slidecontainer {
  width: 100%;
}

.slider, .slider2 {
  -webkit-appearance: none;
  width: 100%;
  height: 10px;
  border-radius: 5px;
  background: #999;
  outline: none;
  opacity: 1;
  -webkit-transition: .2s;
  transition: opacity .2s;
}
<!DOCTYPE html>
<html>
<head>

</head>
<body>
<h3>Strength of Belief</h3>
<div class="slidecontainer">
<div class="slider_left">
    <span id="belief_value"></span>%
</div>
<div class="slider_mid">
    <input type="range" min="1" max="100" value="50" class="slider" id="oldbelief">
</div>
</div>

<h3><i>New</i> Strength of Belief in Negative Cognition</h3>
<div class="slidecontainer">
<div class="slider_right">
    <span id="belief2_value"></span>%
</div>
<div class="slider_mid">
    <input type="range" min="1" max="100" value="50" class="slider2" id="newbelief">
</div>
</div>

</body>
</html>
Timuçin Çiçek
2020-02-14

运行时,我没有看到问题。添加CSS后,我注意到文本确实消失了。但是,在选择了所有内容之后,我弄清楚了问题:您的文本是彩色 #fff ,是白色的。 #000 是黑色。当我将其放在 color 属性中时,它已修复。

154913036
ARI FISHER
2020-02-14