大学课堂作业:
下面是我的页面的html代码。分配的任务是转换滑块范围1-10,以便当有人输入1-4时,将有一个文本段落进行注释。当用户输入6-10时,段落中将出现不同的注释。或者更好的是,每个评分从1到10都有一个单独的评论。
下面是我到目前为止想出来的,但我甚至不确定我是否在正确的轨道上。如何从滑块中获取值?
var ratingSlider=document.getElementById("slider");
ratingSlider=addEventListener("blur", moveSlider);
function moveSlider() {
var sliderField=document.getElementById("slider");
var rating=sliderField.value;
var theCritique="Yeah, man!";
var paragraphField = document.getElementById("critique");
paragraphField.innerHTML = theCritique;
} <div id-"rateUs">
<label>Rate Our Restaurant</label>
<p style="text-align:right;">Excellent</p>
<input id="slider" type="range" min="1" max="10" step="1" name=rateUs>
<p style="text-align:left;">Poor</p>
<p id="critique"></p>
</div>
发布于 2017-11-05 03:20:11
我已经重新组织了你上面的代码,下面的代码会给出你所需要的输出。
var ratingSlider=document.getElementById("slider");
ratingSlider.oninput = function() {
var sliderValue = this.value;
var paragraphField = document.getElementById("critique");
var theCritique = "";
if(sliderValue >= 1 && sliderValue <= 2){
theCritique="very Poor!";
color = "red";
} else if(sliderValue >= 3 && sliderValue <= 4){
theCritique="Poor!";
color = "#da5757";
} else if(sliderValue >= 6 && sliderValue <= 8){
theCritique="Good!";
color = "#66bd66";
} else if(sliderValue >= 9 && sliderValue <= 10){
theCritique="Excellent!";
color = "green";
} else{
theCritique="Okay!";
color = "orange";
}
paragraphField.innerHTML = theCritique;
paragraphField.style.backgroundColor = color;
} <div id="rateUs">
<h1>Rate Our Restaurant</h1>
<p style="float:right;text-align:right;">Excellent</p>
<p style="float:left;text-align:left;">Very Poor</p>
<div style="clear:both;margin:0 auto;width:50%;"><input id="slider" type="range" min="1" max="10" step="1" name="rateUs"></div>
<p id="critique" style="padding:10px;"></p>
</div>
发布于 2017-11-05 02:46:12
您不希望在这里发生blur事件,因为它会在元素失去焦点时触发,而不是在您释放滑块时触发,而是在另一个元素将焦点从滑块上移开时触发。您需要input和/或change事件(对于跨浏览器解决方案,您可能需要这两者)。
您还需要一条if/then语句来确定显示什么消息以及何时显示。
你也有一些语法问题和不必要的代码。详情请参见评论:
// Get the references you need once
var ratingSlider = document.getElementById("slider");
var paragraphField = document.getElementById("critique");
// You had ratingSlider=addEventListener, but it should be:
ratingSlider.addEventListener("input", moveSlider);
function moveSlider() {
var message = "";
// The + right in front of ratingSlider explicitly converts the string
// coming from the HTML value to a number so that a math comparison can be done
if(+ratingSlider.value <= 4){
message = "No way!";
} else {
message = "Yeah, man!";
}
// Don't use .innerHTML when you aren't assigning any HTML, it's wastefull
paragraphField.textContent = message;
}<!-- No need for an id on the div and don't give an element an id
that is the same as the name of another, that just leads to confusion. -->
<div>
<!-- <label> elements are for labeling form fields, not for titles.
If you use a label, you either need to have it contain the element
it is a label for, or use the "for" attribute that has the id of
the element it is labeling. -->
Rate Our Restaurant
<input id="slider" type="range" min="1" max="10" step="1" name="rateUs">
</div>
<p style="float:left;width:20%;margin-left:7em;">Poor</p>
<p style="float:left;width:20%;">Excellent</p>
<p id="critique"></p>
这应该可以帮助您入门,但是如果您对1到10之间的每个值都有不同的注释感兴趣,我将给您一个提示-一个数组可以帮助您做到这一点,并且您将能够删除整个if/then语句。
发布于 2017-11-05 02:50:48
你可以这样做,如果你想添加更多条件,你可以添加更多的else if。
var ratingSlider=document.getElementById("slider");
ratingSlider=addEventListener("blur", moveSlider);
function moveSlider() {
var sliderField=document.getElementById("slider");
var rating=sliderField.value;
var theCritique="";
if(rating <= 4){
theCritique="Yeah, man!";
} else {
theCritique="Another Text!";
}
var paragraphField = document.getElementById("critique");
paragraphField.innerHTML = theCritique;
}<div id-"rateUs">
<label>Rate Our Restaurant</label>
<p style="text-align:right;">Excellent</p>
<input id="slider" type="range" min="1" max="10" step="1" name=rateUs>
<p style="text-align:left;">Poor</p>
<p id="critique"></p>
</div>
https://stackoverflow.com/questions/47114189
复制相似问题