1.封閉函數(shù)
撫順網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)公司!從網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、APP開發(fā)、成都響應式網(wǎng)站建設等網(wǎng)站項目制作,到程序開發(fā),運營維護。創(chuàng)新互聯(lián)公司于2013年開始到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設就選創(chuàng)新互聯(lián)公司。
封閉函數(shù)是JavaScript中匿名函數(shù)的另外一種寫法,創(chuàng)建一個一開始就執(zhí)行而不用命名的函數(shù)
/在封閉函數(shù)前加’;‘,可以避免js壓縮時出錯/
;(function(){
alert('hello world!');
})();
/*當i大于78時等于78,小于時等于89*/
var i = 90>78?78:89;
alert(i);
/*第二個寫法*/
!function(){
alert('hello world!');
}();
/*第三個寫法*/
~function(){
alert('hello world!');
}();
**2.常用內(nèi)置對象**
1.document
document.getElementById 通過id獲取元素
document.getElementByTagName 通過標簽名獲取元素
document.referrer 獲取上一個跳轉(zhuǎn)頁面的地址(需要服務器環(huán)境)
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>內(nèi)置對象</title>
<script type="text/javascript">
window.onload = function(){
/*存儲上一個地址,需要服務器環(huán)境*/
/*var sUrl = document.referrer;*/
var oBtn = document.getElementById('btn01');
oBtn.onclick = function(){
/*window.location.href = sUrl;*/
window.location.;
}
}
</script>
</head>
<body>
<input type="button" name="" value="跳轉(zhuǎn)" id="btn01">
</body>
</html>
2.location
window.location.href 獲取或者重定向url地址
window.location.search 獲取地址參數(shù)部分
window.location.hash 獲取頁面錨點或者叫哈希值
例子:通過地址參數(shù)更換背景
文件一:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>內(nèi)置對象</title>
<script type="text/javascript">
window.onload = function(){
var oBody = document.getElementById('body01');
/*通過?...傳遞地址*/
var sDate = window.location.search;
/*通過#...傳遞地址*/
var sHash = window.location.hash;
/*alert(sHash);*/
if(sDate != ''){
var iRan = sDate.split('='); /*以等號分割*/
var iNum = iRan[1]; /*元素集的第二個*/
if(iNum==1){
oBody.style.backgroundColor = 'gold';
}
else if(iNum==2){
oBody.style.backgroundColor = 'red';
}
else if(iNum==3){
oBody.style.backgroundColor = 'blue';
}
}
}
</script>
</head>
<body id="body01">
<a href="inner01.html">到inner01</a>
</body>
</html>
文件二:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>設置背景</title>
</head><body>
<a href="innerobject.html?a=1#123">背景一</a>
<br /><br />
<a href="innerobject.html?a=2">背景二</a>
<br /><br />
<a href="innerobject.html?a=3">背景三</a>
</body>
</html>
3.Math
Math.random 獲取0-1的隨機數(shù)
Math.floor 向下取數(shù)
Math.ceil 向上取數(shù)
例子:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>math</title>
<script type="text/javascript">
var iNum = Math.PI;
alert(iNum);
var iNum01 = [];
for(var i=0;i<20;i++){
/*返回0-1的隨機數(shù)*/
iNum02 = Math.random();
iNum01.push(iNum02);
}
alert(iNum01);
/*向下取整*/
alert(Math.floor(5.7));
/*向上取整*/
alert(Math.ceil(8.1));
/*產(chǎn)生10-20的隨機數(shù)*/
var iNum03 = 10;
var iNum04 = 20;
var iNum05 = [];
for(var i=0;i<20;i++){
/*因為向下取整,為了出現(xiàn)20,加1,*/
var iN = Math.floor((iNum04-iNum03+1)*Math.random()) + iNum03;
iNum05.push(iN);
}
console.log(iNum05); /*在瀏覽器console里顯示*/
</script>
</head>
<body>
</body>
</html>
4.js調(diào)試方法
(1)alert():會阻止程序的運行
(2)console.log():瀏覽器console
(3)document.title():頁面標題
網(wǎng)頁標題:JavaScript封閉函數(shù)、常用內(nèi)置對象、js調(diào)試方法
標題來源:http://chinadenli.net/article42/ppgdhc.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供標簽優(yōu)化、網(wǎng)站排名、全網(wǎng)營銷推廣、域名注冊、ChatGPT、網(wǎng)頁設計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)