尝试从switch语句输入和输出HTML到JS [英] Trying to input and output from HTML to JS from switchs statements

查看:118
本文介绍了尝试从switch语句输入和输出HTML到JS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我试图创建一个可以回答很多问题的网站。

我在函数getVal()和write()中输出值时遇到问题
孔代码:

Hello ,I was trying to make a site that can answer many questions .
I have issues with output the value in the function "getVal()" and "write()"
The hole code :

<html>
<style>
p {
font-size:30px;
padding:3px;
font-family:Terminal;
}
#lable{
border:2px black dashed;
margin:0px 400px 0px 400px;
background-color:lightgrey; padding-bottom:150px;
}
#input {
background-color:lightgrey;
margin:15px; 
padding-left:100px;
padding-right:100px;
padding-bottom:15px;
}
#send {
padding:3px 3px 3px 3px;
}
#delete {padding:3px 3px 3px 3px;}

</style>
<body style="text-align:center; background-color:grey; color:white;
">
<img src="Alexs_face.png">
<p style="border:2px black solid; margin:100px 400px 50px 400px">Ask me !</p>
<p id="lable"></p>
<input id="input" type="text" >
<input id="send" type="button" >
<input id="delete" type="button" >
<script>

function getVal() {
	var ask = document.getElementById("input").value;
	return ask ;
}
var ask = getVal();

//var ask = prompt ("Ask me anything >>").toLowerCase ();
function write (x){
	var lable = document.getElementById("lable").value = x;
return lable ;
	};

//Capitalize the first letter func :
function capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
//..............

var question = ask.split(" ");
var date = new Date ;
//var userName ;


   //...............................
   
if (question[0] === "what"){

switch ( question[1]){
    case "time":
                switch (question[2]){
                    case "is":
                              switch (question[3]){
                              case "it":
                              write("Hi , the time is :"+date);
                              break;
                              default:
                              };
                    break;
                default:    
                } ;
     break;
    case "is":
              switch (question[2]){
                  case "your" :
                               switch (question[3]){
                               case "name":
                               write("Alex !");
                               break;
                               default:
                               };
                  break;
                  default:

              };
    break;

default: write("unknown");

};}
else if (question[0] === "my"){

switch (question[1]){
    case "name":
               switch(question[2]){
                   case "is":

                    userName = capitalize(question[3]);;
                    alert("Your name is saved, "+userName);
                    function sendUserName(){

                        return userName ;
                    }
                   break;

                  default: 
               };
    break;



    default:
};
}
else if (question[0] === "should" || "could" || "may" || "can" )  {


switch (question[1]) {
case "i" :
          switch(question[2]){
		  case "sleep":
		  write("Sure ! you can sleep if you want to !!");
		  break;
		  default:
		  }
break;

default:
};


}
if (question[0] === "who"){

switch (question[1]){

case "are":
write ("I'm Alex !");
break;
 case "am":
 write ("My leader !");
default:
}

}
else if (question[0] === "i") {
switch(question[1]){
case "love":
switch(question[2]){
case "you" :
write ("I love you too , sir")
break;

}
break;
};
}



</script>
</body>


</body>

</html>

</script>
<html>





我尝试过:



我尝试了我想到的一切。

请忽略代码中的注释//



What I have tried:

I tried everything that I thought of .
please ignore the comments in the code //

推荐答案

做这些改变



do these changes

<input id="send" value="send" onclick="dosome()" type="button" >







function write (x){
	  document.getElementById("lable").innerHTML = x;
 
	};










function dosome() 
 {
 var ask = getVal();
var question = ask.split(" ");
var date = new Date ;
//var userName ;
 

   //...............................
   
if (question[0] === "what"){
 
switch ( question[1]){
    case "time":
                switch (question[2]){
                    case "is":
                              switch (question[3]){
                              case "it":
                              write("Hi , the time is :"+date);
                              break;
                              default:
                              };
                    break;
                default:    
                } ;
     break;
    case "is":
              switch (question[2]){
                  case "your" :
                               switch (question[3]){
                               case "name":
                               write("Alex !");
                               break;
                               default:
                               };
                  break;
                  default:
 
              };
    break;
 
default: write("unknown");
 
};}
else if (question[0] === "my"){
 
switch (question[1]){
    case "name":
               switch(question[2]){
                   case "is":
 
                    userName = capitalize(question[3]);;
                    alert("Your name is saved, "+userName);
                    function sendUserName(){
 
                        return userName ;
                    }
                   break;
 
                  default: 
               };
    break;
 

 
    default:
};
}
else if (question[0] === "should" || "could" || "may" || "can" )  {
 

switch (question[1]) {
case "i" :
          switch(question[2]){
		  case "sleep":
		  write("Sure ! you can sleep if you want to !!");
		  break;
		  default:
		  }
break;
 
default:
};
 

}
if (question[0] === "who"){
 
switch (question[1]){
 
case "are":
write ("I'm Alex !");
break;
 case "am":
 write ("My leader !");
default:
}
 
}
else if (question[0] === "i") {
switch(question[1]){
case "love":
switch(question[2]){
case "you" :
write ("I love you too , sir")
break;
 
}
break;
};
}
}


这篇关于尝试从switch语句输入和输出HTML到JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆