1. 程式人生 > >JavaScript的內建物件

JavaScript的內建物件

1.內建函式 eval()和 isNaN()
Eval(): 用於計算字串表示式的值;
IsNaN(): 使用者驗證引數是否是 NaN(非數字);
eg:

<script type="text/javascript">
	//eval():js的內建函式用來計算表示式的值
	var mess=prompt('輸入一個表示式','12*2');
	var value=eval(mess);
	alert(value);
	//isNaN():判斷是一個非數字的值  true/false
	var mess1=prompt('請輸入','0');
	if(isNaN(mess1)){  //判斷 是一個非數字 的值
		alert('是一個非數字的值');
	}else{
		alert('不是一個非數字的值');
	}
</script>

2.瀏覽器物件模型包含的三大物件有:
瀏覽器物件: 瀏覽器視窗window物件; 文件document物件;History物件;Location物件.
|
指令碼物件: String物件;Date物件;Math物件;Array物件.
|
HTML物件: 任何一個html元素 都可以稱之為HTML物件.
3.介紹
(1)Math物件
屬性::
在這裡插入圖片描述
方法:
在這裡插入圖片描述
eg:

<script type="text/javascript">
	//Math.random(): 隨機產生0-1之間的數字
	//Math.round(): 四捨五入取整
	//問題: 使用計時器以及Math物件實現頁面2秒 顯示不同的圖片
	function suiji(){
		var mathValue=Math.round(Math.random()*8+1);
		document.getElementById('img1').src='img/'+mathValue+'.jpg';
	}
	setInterval('suiji()',1000);
</script>
...............
<body>
	<img src="img/1.jpg" id="img1" style="width: 1300px;height: 600px;display:inline-block;margin-left: 25px;margin-top: 25px;"/>
</body>

當然ing資料夾裡要有圖片
在這裡插入圖片描述
(2).Date物件:
Date 物件儲存的日期為自1970年1月1日 00:00:00 以來的毫 秒數 .
Date 方法的分組:
在這裡插入圖片描述
用作 Date 方法的引數的整數:
在這裡插入圖片描述
Get 方法:
在這裡插入圖片描述
eg:

<script type="text/javascript">
	//Date() 物件中的日期格式 如下;
	var dateDay=new Date('2018/12/12');//第一種書寫格式
	//var dateDay=new Date('wed dec 1 2019');//第二種書寫格式
	//alert(dateDay);
	
	var time=new Date();
	//alert(time.getDate());// 獲取月份中的天數
	//alert(time.getFullYear()); //獲取年份
	//alert(time.getDay()); //獲取星期幾\
	//alert(time.getMilliseconds());//  獲取毫秒數
	//alert(time.getMonth()+1);//獲取月份數
	//alert(time.getTime());
</script>

(3).Window 物件:
open(”開啟視窗的 url”,”視窗名”,”視窗特徵”)
視窗的特徵如下,可以任意組合: height: 視窗高度;
width: 視窗寬度;
top: 視窗距離螢幕上方的象素值;
left:視窗距離螢幕左側的象素值;
close()關閉當前視窗.
eg:

<script type="text/javascript">
	var left1=0;
	var top1=0;
	function load(){
		//var left=Math.random()*500;
		//var top=Math.random()*300;
		left1=Math.random()*500;
		top1=Math.random()*300;
		//left1+=50;
		//top1+=50;
		var div=document.getElementById('Layer1');
		div.style.left=left1+'px';
		div.style.top=top1+'px';
	}
	setInterval('load()',500);
</script>
......................
<body>
	<div  id="Layer1" style="position:absolute; left:14px; top:44px; width:150px; height:102px; z-index:1">
	  		<a href="http://www.mycom.cn"><IMG src="img/1.jpg" width="150" height="100" border="0"></a>
	</div>
	<h1>隨機漂浮的廣告</h1>
</body>

(4).Document 物件:
屬性:
在這裡插入圖片描述
方法:
在這裡插入圖片描述
eg:

<script type="text/javascript">
	function createNewDoc(){
		 var newDoc=document.open("01實現打字機效果.html","replace");
		 var txt="<html><body>Learning about the DOM is FUN!</body></html>";
		 newDoc.write(txt);
		 newDoc.close();
  	}
</script>
....................
<body>
	<input type="button" value="Write to a new document"onclick="createNewDoc()">
</body>

(5).History 物件:

名稱 說明
back() 載入 History 列表中的上一個 URL.
forward() 載入 History 列表中的下一個 URL.
go("url"ornumber) 載入 History 列表中的一個 URL,或要求瀏覽器移動指定的頁面數.

back ( ) 方法相當於後退按鈕;
forward ( ) 方法相當於前進按鈕;
go (1)代表前進 1 頁,等價於 forward( )方法;
go(-1) 代表後退 1 頁,等價於 back( )方法;
(6).Loaction 物件:
屬性:

名稱 說明
host 設定或檢索位置或 URL 的主機名和埠號
hostname 設定或檢索位置或 URL 的主機名部分
href 設定或檢索完整的 URL 字串

方法:

名稱 說明
assign(“url”) 載入 URL 指定的新的 HTML 文件.
reload() 重新載入當前頁
replace(“url”) 通過載入 URL 指定的文件來替換當前文件

eg:

<script type="text/javascript">
	//alert(location.host);//獲取或是設定主機的埠號和主機名稱  127.0.0.1:8020
	//alert(location.hostname);
	//alert(location.href); //設定或是檢索url資訊
	
	//location.assign('http://www.yltedu.com');  //載入一個新的url地址
	//location.reload(true);  //重新載入當前頁面
	//location.replace('new_file.html');  //使用一個新的url替換當前的url
</script>