JS技术

javascript代码的封装_Javascript教程(2)

字号+ 作者:H5之家 来源:H5之家 2015-10-02 09:04 我要评论( )

table id=theTable_1 width=100% border=0 cellspacing=0 cellpadding=0 tr tdnbsp;/td td align=left width=20%Animal weight:/td td align=leftinput type=text id=inputObj_x_id name=inputObj_x_name value=1

 <table id="theTable_1" width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
   <td>&nbsp;</td>
   <td align="left" width="20%">Animal weight:</td>
   <td align="left"><input type="text" id="inputObj_x_id" name="inputObj_x_name" value="1" title="必须输入0-9999内的数字"/></td>
  </tr>
  <tr>
   <td>&nbsp;</td>
   <td align="left" width="20%">Bird weight:</td>
   <td align="left"><input type="text" id="inputObj_y_id" name="inputObj_y_name" value="10" title="必须输入0-9999内的数字"/></td>
  </tr>
  <tr>
   <td>&nbsp;</td>
   <td align="left" width="20%">People weight:</td>
   <td align="left"><input type="text" id="inputObj_z_id" name="inputObj_z_name" value="100" title="必须输入0-9999内的数字"/></td>
  </tr>
  <tr>
   <td colspan="3">
    <input type="button" class="button" onclick="viewResult() ;" value="查看结果" />
    <input type="button" class="button" onclick="removeConsole();" value="清除控制台" />
   </td>
  </tr>
 </table>
 <br>
 <br>
 <table id="theTable_2" width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr class="headRow">
   <td>console[信息台]</td>
  </tr>
 </table>
 <br>
</BODY>
<div id="console_id" style="height:150;overflow-y:auto;width:100%;"></div>
<SCRIPT language="javascript">
 var inputObj_x = document.getElementById("inputObj_x_id");//x input object
 var inputObj_y = document.getElementById("inputObj_y_id");//y input object
 var inputObj_z = document.getElementById("inputObj_z_id");//z input object
 var consoleObj = document.getElementById("console_id");//console div object

//打印信息到控制台
function printMsg2Console(message,color){
 if(message == 'undefined') return;
 if(!color) color = 'black';

 if(consoleObj == 'undefined')
  consoleObj = document.getElementById("console_id");

 var newChild = document.createElement("<span>");
 newChild.innerText = message;
 consoleObj.appendChild( newChild );
 newChild = document.createElement("<br>");
 consoleObj.appendChild( newChild );
 newChild.scrollIntoView(true);
}

 //清除控制台的信息
 function removeConsole(){
  if(consoleObj == 'undefined')
   consoleObj = document.getElementById("console_id");

   consoleObj.innerHTML = "";
 }

 //查看 动物, 鸟, 人 对象的结果
 function viewResult(){
  //打印全局变量 以及 函数执行结果
  var x = inputObj_x.value;
  var y = inputObj_y.value;
  var z = inputObj_z.value;
 
  var animal = new Animal(x);
  var bird = new Bird(y);
  var people = new People(z);
 
  var result = "animal \tweight = " + animal.weight + "; eat = " + animal.eat() + "; move = " + animal.move()
   +  "\nbird \t\t\tweight = " + bird.weight + "; eat = " + bird.eat() + "; move = " + bird.move()
   +  "\npeople \tweight = " + people.weight + "; eat = " + people.eat() + "; move = " + people.move();

  printMsg2Console(result, "black");

 }

</SCRIPT>
</HTML>

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • Javascript代码技_Javascript教程

    Javascript代码技_Javascript教程

    2015-09-26 08:00

网友点评