初學(xué)JavaScript(js基礎(chǔ))

一、將腳本放在哪里

腳本可以放在html頁面的兩個位置:
腳本總是需要包圍在<script></script>html標(biāo)簽之間。
1.<head></head>標(biāo)簽之間(稱為頭腳本);
2.<body></body>標(biāo)簽之間(稱為體腳本);

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>My first javascript</title>
  </head>
  <body >
    <style type="text/css">
    body {background-color:red;}
    </style>
    <h1>
      <script type="text/javascript">  //script開始標(biāo)簽,告訴瀏覽器后面代碼為javascript而不是html
        document.write("hello,world"); //分號告訴瀏覽器這一行結(jié)束了
      </script>  //結(jié)束javascript,表示后面為html代碼
    </h1>
  </body>
</html>

output:

腳本

二、函數(shù)

函數(shù)由function加上函數(shù)名組成。函數(shù)名后面是圓括號,在后面是左花括號。組成函數(shù)內(nèi)容的語句出現(xiàn)在后面的行上,然后在用右花括號結(jié)束這個函數(shù)。

function saySomething () {
    alert (" Four score and seven years ago")
}

三、使用外部腳本

<!DOCTYPE html>
  <html> 
    <head> 
      <meta charset="utf-8"> 
      <meta name="viewport" content="width=device-width"> 
      <title>My second javascript</title> 
    </head>
    <body >
     <style type="text/css">
       body {background-color:red;} 
     </style> 
      <h1 id="helloMessage"> 
        <script type="text/javascript" src="script2.js">  </script>//在script標(biāo)簽中添加src屬性,可以調(diào)用.js文件
      </h1> 
    </body>
  </html>

script2.js代碼:

window.onload=writeMessage;  //當(dāng)窗口完成加載時,運行writeMessage 函數(shù)
function writeMessage () {    //創(chuàng)建函數(shù)“writeMessage () ”
    document.getElementById ("helloMessage").innerHTML="hello,world";
}

四、向用戶發(fā)出警告

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>my js page</title>
  <script type="text/javascript" src="script3">
  </script>
</head>
<body>
  <noscript>  //在不支持javascript的瀏覽器上,會顯示一條消息“This page requires Javascript”
    <h2>This page requires Javascript</h2>
  </noscript>
</body>
</html>

script3:

alert("Welcome to my Javascript page!");

output:

javascript page

五、確認(rèn)用戶的選擇(條件語句)

if (confirm("Are you sure you want to do that?")){
  alert("You said yes");
}
else{
    alert("You said no");
}

output:


conrirme
then
else

結(jié)構(gòu):

if(confirm()){
   alert();    //then部分,表示返回true值時執(zhí)行的代碼;
}
else{
   alert();    //表示返回值為false值時執(zhí)行的代碼;
}

六、提示用戶

var ans=prompt("Are you sure you want to do that?","");//var變量關(guān)鍵字;ans變量;prompt詢問;用逗號分隔兩段信息,向用戶詢問的問題和默認(rèn)回答;
if(ans){
  alert("You said "+ans);//返回用戶的響應(yīng)
}
else {
  alert("You refused to answer");//ans為null
}

output:


prompt
ans
null

七、用鏈接對用戶進(jìn)行重定向

1.以下html頁面基于鏈接隊用戶進(jìn)行重定向;

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Welcome to our site</title>
  <script type="text/javascript" src="script4">
  </script>
</head>
<body>
  <h1 align="center">
    <a href="script5.html" id="redirect">
      Welcome to our site</a>
  </h1>
  <noscript>
    <h2>This page requires Javascript</h2>
  </noscript>
</body>
</html>

2.通過重定向功能嵌入在代碼中,用戶甚至不知道你的腳本干預(yù)了鏈接的行為;

window.onload=initAll;
function initAll(){
  document.getElementById("redirect").onclick=initRedirect;
}

function initRedirect() {
  window.location="welcome.html";
  return false;
}

3.以下時啟用了javascript功能的用戶將看到的另一個html頁面;

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Our Site</title>
  <script type="text/javascript" src="script4">
  </script>
</head>
<body>
  <h1 >
      Welcome to our web site,
    which features lots of cutting-edge Javascript.
  </h1>
</body>
</html>

七、使用多級條件

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容