一篇文章带你快速入门JavaScript(实操代码)
目錄
- 一、前提簡介
- 1.1什么是JavaScript
- 1.2JavaScript和Java語言的區別
- 1.3Html、Css和Javascript
- 1.4Javascript作用
- 二、實操代碼
- 2.1Javascript寫在本html內
- 2.2Javascript可以寫在單獨的文件中(外聯方式)
- 2.3實戰:點擊一個盒子,讓另外一個盒子變色
- 2.4實戰:一個按鈕綁定一個事件
- 2.4實戰:變換皮膚
一、前提簡介
1.1什么是JavaScript
JavaScript是一種動態的計算機編程語言。它是輕量級的,最常用作網頁的一部分,其實現允許客戶端腳本與用戶交互并創建動態頁面。它是一種具有面向對象功能的解釋型編程語言。
1.2JavaScript和Java語言的區別
Javascript和Java沒有任何關系,它們是不同的兩種語言(java是一種程序設計語言,javascript 是客戶端的腳本語言),只是名字上都有一個Java而已。
1.3Html、Css和Javascript
這三個要素共同構成了Web開發的基礎。
HTML:頁面的結構-標題,正文,要包含的任何圖像
CSS:控制該頁面的外觀(這將用于自定義字體,背景顏色等)
JavaScript:不可思議的第三個元素。創建結構(HTML)和美學氛圍(CSS)后,JavaScript使您的網站或項目充滿活力。
1.4Javascript作用
(上面這個作用是直接用的我的老師的課件,我可沒這么6懂這么多。他一個10多年開發經驗的資深程序員哈哈哈哈哈哈,有點想幫忙宣傳一下他的網課,但想想還是算了吧,感覺打廣告有點不好)
*********************************************一條華麗的分割線***************************************************
二、實操代碼
2.1Javascript寫在本html內
1. js程序必須寫在script標簽中。
2. script:可以寫在網頁中的任何位置。
3. type=“text/javascript”:表示當前的語言是javascript語言。這個屬性是可以省略的。
舉例:上代碼
<!DOCTYPE html> <html><head><meta charset="utf-8" /><title></title></head><body><script type="text/javascript">alert("出錯啦")</script></body> </html>拿代碼去運行一下就知道了
2.2Javascript可以寫在單獨的文件中(外聯方式)
創建一個js文件,在js文件中編寫js代碼。(外部文件中編寫js代碼就直接寫代碼就可以了,不用再添加script標簽)
比如說在js目錄下面創建一個 test.js文件 里面的代碼為alert(“出錯啦!”)
舉例上代碼
a.html
<!DOCTYPE html> <html><head><meta charset="UTF-8"><title></title></head><body><script src="js/test.js" type="text/javascript" charset="UTF-8"></script></body> </html>拿代碼去運行一下就知道了
2.3實戰:點擊一個盒子,讓另外一個盒子變色
舉例上代碼:
<!DOCTYPE html> <html><head><meta charset="utf-8"><title></title><style type="text/css">#box1{width: 100px;height: 100px;background-color: red;}#box2{width: 100px;height: 100px;background-color: blue;}</style></head><body><div id="box1"></div><div id="box2"></div><script type="text/javascript">//目標:點擊box1時,讓box2變顏色var b1 = document.getElementById("box1")b1.onclick=function(){// 當點擊b1的時候,執行此處的代碼document.getElementById("box2").style.backgroundColor="pink"}</script></body> </html>運行效果拿去試試就知道了,點一下第一個小盒子
2.4實戰:一個按鈕綁定一個事件
舉例上代碼
<!DOCTYPE html> <html><head><meta charset="UTF-8"><title></title></head><body><input type="text" name="tb1" id="tb1" value="" />+<input type="text" name="tb2" id="tb2" value="" /> =<input type="text" name="tb3" id="tb3" value="" /><input type="button" id="btnjisuan" value="計算" onclick="add();" /><a href="javascript:void(0);" onclick="bb();">騰訊</a><script type="text/javascript">function add(){var v1=document.getElementById("tb1").value;var v2=document.getElementById("tb2").value;var v3=parseInt(v1) + parseInt(v2);document.getElementById("tb3").value=v3;}function bb(){location.href="http://www.qq.com"; //通過js代碼實現頁面的跳轉 }</script></body> </html>拿去運行一個就知道了哈哈哈哈,這個學會了,下面那個就容易多啦!
*********************************************一條華麗的哈哈哈哈哈哈哈哈***************************************************
2.4實戰:變換皮膚
實現效果:點擊什么顏色代表的小框框,就會彈出穿啥衣服的 fairy
(哈哈哈哈 本人敲愛看這些美麗的事物哈哈哈哈)
https://blog.csdn.net/hanhanwanghaha一個超級無敵可愛的人鴨
歡迎您的關注! 歡迎關注微信公眾號:寶藏女孩的成長日記 如有轉載,請注明出處(如不注明,盜者必究)
自己可以下載一些圖片或者顏色漸變圖片用來做背景,放在img里面,可自己命名。基本格式如下圖:
上代碼:
網頁換膚.html
<!DOCTYPE html> <html><head><meta charset="UTF-8"><title></title><link rel="stylesheet" type="text/css" href="css/css2.css" id="btnlink"/></head><body><div id="box1"><span id="s1" onclick="a1();">志玲</span><span id="s2" onclick="a2();">依林</span><span id="s3" onclick="a3();">昆凌</span></div><script type="text/javascript">function a1(){document.getElementById("btnlink").href="css/css1.css";}function a2(){document.getElementById("btnlink").href="css/css2.css";}function a3(){document.getElementById("btnlink").href="css/css3.css";}</script></body> </html>css1.css
*{margin: 0;padding: 0; }html,body{width:100%;height: 100%; }body{background-image: url(../img/blue.jpg);background-repeat: repeat-x; /* 設置不重復平鋪 */ }#box1{width: 186px;height: 60px;background-color: white;margin: 0 auto;position: relative; } #s1{width: 60px;height: 60px;background-color: blue;display: inline-block;margin: 1px;cursor: pointer;position: absolute; /* 子絕父相 */left: 0;top: 0; } #s2{width: 60px;height: 60px;background-color:green;display: inline-block;margin: 1px;cursor: pointer;position: absolute;left: 62px;top: 0; } #s3{width: 60px;height: 60px;background-color: pink;display: inline-block;margin: 1px;cursor: pointer;position: absolute;right: 0;top: 0; }css2.css
*{margin: 0;padding: 0; }html,body{width:100%;height: 100%; }body{background-image: url(../img/green.jpg) }#box1{width: 186px;height: 60px;background-color: white;margin: 0 auto;position: relative; } #s1{width: 60px;height: 60px;background-color: blue;display: inline-block;margin: 1px;cursor: pointer;position: absolute; /* 子絕父相 */left: 0;top: 0; } #s2{width: 60px;height: 60px;background-color:green;display: inline-block;margin: 1px;cursor: pointer;position: absolute;left: 62px;top: 0; } #s3{width: 60px;height: 60px;background-color: pink;display: inline-block;margin: 1px;cursor: pointer;position: absolute;right: 0;top: 0; }css3.css
*{margin: 0;padding: 0; }html,body{width:100%;height: 100%; }body{background-image: url(../img/pink.jpg) }#box1{width: 186px;height: 60px;background-color: white;margin: 0 auto;position: relative; } #s1{width: 60px;height: 60px;background-color: blue;display: inline-block;margin: 1px;cursor: pointer;position: absolute; /* 子絕父相 */left: 0;top: 0; } #s2{width: 60px;height: 60px;background-color:green;display: inline-block;margin: 1px;cursor: pointer;position: absolute;left: 62px;top: 0; } #s3{width: 60px;height: 60px;background-color: pink;display: inline-block;margin: 1px;cursor: pointer;position: absolute;right: 0;top: 0; }一些很基礎的東西,要是寫起來那就太多了,很多不常用的,到了我們需要它的時候谷歌和百度就行了。
由于時間關系,暫時更到這里(自己敲了一下老師上課講的精華部分),后面如果有補充的話我會及時更,嘿嘿嘿
歡迎關注
https://blog.csdn.net/hanhanwanghaha
一個超級無敵可愛的人鴨
期待一起學習嗷嗷。
總結
以上是生活随笔為你收集整理的一篇文章带你快速入门JavaScript(实操代码)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Windows Azure 将正式更名为
- 下一篇: Spring 实战-第一章-基本概念