为什么此范围选择器不显示值?

时间:2018-03-11 00:26:21

标签: javascript html css

注意:以下是我在网上找到的范围滑块。虽然,它在线工作 - codepen,它在移动滑块时动态显示值。

错误: 未捕获的TypeError:无法读取null的属性“addEventListener”     在javascriptFile.js:9

建议1:

Move the code of rangeValue function inside if clause : if (elem)
result : the function is not even being executed though the error disappears.
  

怎么样?

     

尝试在函数内添加console.log(“hi”),但事实并非如此   被打印。

参考: working link on codepen

HTML CODE

<div class="value">0</div>
<input type="range" min="0" max="100" step="1" value="0">

CSS代码

@import url(https://fonts.googleapis.com/css?family=Dosis:700);
body {
    font-family: "Dosis", Helvetica, Arial, sans-serif;
    background: #ecf0f1;
    color: #34495e;
    padding-top: 40px;
    text-shadow: white 1px 1px 1px;
}
.value {
    border-bottom: 4px dashed #bdc3c7;
    text-align: center;
    font-weight: bold;
    font-size: 10em;
    width: 300px;
    height: 100px;
    line-height: 60px;
    margin: 40px auto;
    letter-spacing: -0.07em;
    text-shadow: white 2px 2px 2px;
}
input[type="range"] {
    display: block;
    -webkit-appearance: none;
    background-color: #bdc3c7;
    width: 300px;
    height: 5px;
    border-radius: 5px;
    margin: 0 auto;
    outline: 0;
}
input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    background-color: #e74c3c;
    width: 30px;
    height: 30px;
    border-radius: 50%;
    border: 2px solid white;
    cursor: pointer;
    transition: 0.3s ease-in-out;
}
​ input[type="range"]::-webkit-slider-thumb:hover {
    background-color: white;
    border: 2px solid #e74c3c;
}
input[type="range"]::-webkit-slider-thumb:active {
    transform: scale(1.6);
}

JS代码

var elem = document.querySelector('input[type="range"]');

var rangeValue = function(){
  var newValue = elem.value;
  var target = document.querySelector('.value');
  target.innerHTML = newValue;
}

elem.addEventListener("input", rangeValue);

0 个答案:

没有答案