首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >初学者HTML和Javascript:将输入滑块值更改为paragraphField.innerHTML

初学者HTML和Javascript:将输入滑块值更改为paragraphField.innerHTML
EN

Stack Overflow用户
提问于 2017-11-05 02:35:23
回答 3查看 39关注 0票数 0

大学课堂作业:

下面是我的页面的html代码。分配的任务是转换滑块范围1-10,以便当有人输入1-4时,将有一个文本段落进行注释。当用户输入6-10时,段落中将出现不同的注释。或者更好的是,每个评分从1到10都有一个单独的评论。

下面是我到目前为止想出来的,但我甚至不确定我是否在正确的轨道上。如何从滑块中获取值?

代码语言:javascript
运行
复制
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;
}	
代码语言:javascript
运行
复制
<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>

EN

回答 3

Stack Overflow用户

发布于 2017-11-05 03:20:11

我已经重新组织了你上面的代码,下面的代码会给出你所需要的输出。

代码语言:javascript
运行
复制
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;
}	
代码语言:javascript
运行
复制
<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>

https://jsfiddle.net/vijayabal/Lr3s1qyo/1/

票数 1
EN

Stack Overflow用户

发布于 2017-11-05 02:46:12

您不希望在这里发生blur事件,因为它会在元素失去焦点时触发,而不是在您释放滑块时触发,而是在另一个元素将焦点从滑块上移开时触发。您需要input和/或change事件(对于跨浏览器解决方案,您可能需要这两者)。

您还需要一条if/then语句来确定显示什么消息以及何时显示。

你也有一些语法问题和不必要的代码。详情请参见评论:

代码语言:javascript
运行
复制
// 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;
}
代码语言:javascript
运行
复制
<!-- 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语句。

票数 0
EN

Stack Overflow用户

发布于 2017-11-05 02:50:48

你可以这样做,如果你想添加更多条件,你可以添加更多的else if

代码语言:javascript
运行
复制
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;
}
代码语言:javascript
运行
复制
<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>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47114189

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档