舊文件

此處文件僅供參考,請自行考量時效性與適用程度,其他庫藏文件請參考文件頁面
我們亟需您的協助,進行共筆系統搬移、及文件整理工作,詳情請查閱參與我們

「使用者:Shyangs/JavaScript」修訂間的差異

出自 MozTW Wiki

字串轉數值
行 101: 行 101:
 
  </body></html>
 
  </body></html>
  
 +
==型態轉換==
 +
===字串轉數值===
 +
*刪去非數字的字串後部,把餘下的數字字串轉換為數值型態。
 +
<html><body>
 +
<span style="color:#704214"><script type="text/javascript"><nowiki><!--</nowiki>
 +
<span style="color:blue">function</span> change(fObj)
 +
{
 +
  n1=parseInt(fObj.str.value);
 +
  n2=parseFloat(fObj.str.value);
 +
  fObj.result1.value=n1;
 +
  fObj.result2.value=n2;
 +
}
 +
//--></script></span>
 +
 +
字串轉數值<nowiki><br></nowiki>
 +
<form>
 +
數值 =<input type=<span style="color:#DE3163">"text"</span> value=<span style="color:#DE3163">"12.56公分78"</span> name=<span style="color:#DE3163">"str"</span>><nowiki><br></nowiki>
 +
parseInt:<input type=<span style="color:#DE3163">"text"</span> value=<span style="color:#DE3163">"輸出"</span> name=<span style="color:#DE3163">"result1"</span>><nowiki><br></nowiki>
 +
parseFloat:<input type=<span style="color:#DE3163">"text"</span> value=<span style="color:#DE3163">"輸出"</span> name=<span style="color:#DE3163">"result2"</span>><nowiki><br></nowiki>
 +
<input type=<span style="color:#DE3163">"button"</span> value=<span style="color:#DE3163">"計算"</span> onClick=<span style="color:#DE3163">"change(this.form)"</span>>
 +
</form>
 +
</body></html>
 +
===字串轉數值===
 +
==eval==
 
=網路資料=
 
=網路資料=
 
*Mozilla 開發者中心:[http://developer.mozilla.org/zh_tw/docs/%E9%87%8D%E6%96%B0%E4%BB%8B%E7%B4%B9_JavaScript 重新介紹 JavaScript]  
 
*Mozilla 開發者中心:[http://developer.mozilla.org/zh_tw/docs/%E9%87%8D%E6%96%B0%E4%BB%8B%E7%B4%B9_JavaScript 重新介紹 JavaScript]  

於 2008年5月24日 (六) 00:55 的修訂

入門基礎

Hello! World!

  • 網頁寫入文字"你好!"。
<html><body>
<script type="text/javascript"><!--
document.write("你好!");
//--></script>
</body></html>


  • 彈出對話框"你好!"。
<html><body>
<script type="text/javascript"><!--
alert("你好!");
//--></script>
</body></html>

資料型態(Data type)

  • Number (數字)
  • String (字串)
  • Boolean (真假值)
  • Object (物件)
    • Function (函式)
    • Array (陣列)
    • Date (日期)
    • RegExp
  • Null (空)
  • Undefined (未定義)

變數(Variable)

  • var 來宣告變數,可以省略關鍵字 var;全域變數與區域變數。
<html><body>
<script type="text/javascript"><!--
var gNum = 123;     //全域變數
gInteger = 20;      //全域變數
var gStr = "字串";  //全域變數
function func()
{
   num = 33;       //區域變數
   str = "文字";   //區域變數
}
//--></script>
</body></html>

流程控制

if 條件判斷

if (條件式) 
{
 陳述句;
}
else 
{
 陳述句;
}

switch 條件判斷

for 迴圈

while 迴圈

break、 continue、goto

數值運算

餘數
+ - * / %


兩數加減乘除

<html><body>
<script type="text/javascript"><!--
function calc(fObj)
{
  n1=parseFloat(fObj.num1.value);
  n2=parseFloat(fObj.num2.value);
  fObj.result1.value=n1 + n2;
  fObj.result2.value=n1 - n2;
  fObj.result3.value=n1 * n2;
  fObj.result4.value=n1 / n2;
  fObj.result5.value=n1 % n2;
} 
//--></script>

數值運算<br>
<form>
數值 a =<input type="text" value="12" name="num1"><br>
數值 b =<input type="text" value="5" name="num2"><br>
兩數之和=<input type="text" value="輸出" name="result1"><br>
兩數之差=<input type="text" value="輸出" name="result2"><br>
兩數乘積=<input type="text" value="輸出" name="result3"><br>
   商=<input type="text" value="輸出" name="result4"><br>
  餘數=<input type="text" value="輸出" name="result5"><br>
<input type="button" value="計算" onClick="calc(this.form)">
</form>
</body></html>

型態轉換

字串轉數值

  • 刪去非數字的字串後部,把餘下的數字字串轉換為數值型態。
<html><body>
<script type="text/javascript"><!--
function change(fObj)
{
  n1=parseInt(fObj.str.value);
  n2=parseFloat(fObj.str.value);
  fObj.result1.value=n1;
  fObj.result2.value=n2;
} 
//--></script>

字串轉數值<br>
<form>
數值 =<input type="text" value="12.56公分78" name="str"><br>
parseInt:<input type="text" value="輸出" name="result1"><br>
parseFloat:<input type="text" value="輸出" name="result2"><br>
<input type="button" value="計算" onClick="change(this.form)">
</form>
</body></html>

字串轉數值

eval

網路資料

個人工具