-
confirm 消息對話框通常用于允許用戶做選擇的動作,如:“你對嗎?”等。彈出對話框(包括一個確定按鈕和一個取消按鈕)。
<script type="text/javascript">
? ?var mymessage=confirm("你喜歡JavaScript嗎?");
? ?if(mymessage==true)
? ?{ ? document.write("很好,加油!"); ? }
? ?else
? ?{ ?document.write("JS功能強大,要學習噢!"); ? }
</script>
查看全部 -
1. function定義函數(shù)的關(guān)鍵字。
2. "函數(shù)名"你為函數(shù)取的名字。
3. "函數(shù)代碼"替換為完成特定功能的代碼。
我們來編寫一個實現(xiàn)兩數(shù)相加的簡單函數(shù),并給函數(shù)起個有意義的名字:“add2”,代碼如下:
function add2(){
? var sum = 3 + 2;
? alert(sum);
}查看全部 -
<script type="text/javascript"> ? var myage = 18;
? if(myage>=18) ?//myage>=18是判斷條件 ? { document.write("你是成年人。");} ? else ?//否則年齡小于18 ? { document.write("未滿18歲,你不是成年人。");}</script>查看全部 -
我們需在HTML中添加如下代碼,就可將JS文件嵌入HTML文件中。
<script src="script.js"></script>
現(xiàn)在在script.js文件中寫入document.write("引用JS文件!");?,JS代碼就直接運行了。
查看全部 -
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" Content="text/html; charset=utf-8" />
<title>javascript</title>
<style type="text/css">
body{font-size:12px;}
txt{
? ? height:400px;
? ? width:600px;
border:#333 solid 1px;
padding:5px;}
p{
line-height:18px;
text-indent:2em;}
</style>
</head>
<body>
? <h2 id="con">JavaScript課程</H2>
? <div id="txt">?
? ? ?<h5>JavaScript為網(wǎng)頁添加動態(tài)效果并實現(xiàn)與用戶交互的功能。</h5>
? ? ? ? <p>1. JavaScript入門篇,讓不懂JS的你,快速了解JS。</p>
? ? ? ? <p>2. JavaScript進階篇,讓你掌握JS的基礎(chǔ)語法、函數(shù)、數(shù)組、事件、內(nèi)置對象、BOM瀏覽器、DOM操作。</p>
? ? ? ? <p>3. 學完以上兩門基礎(chǔ)課后,在深入學習JavaScript的變量作用域、事件、對象、運動、cookie、正則表達式、ajax等課程。</p>
? </div>
? <form>
? <!--當點擊相應(yīng)按鈕,執(zhí)行相應(yīng)操作,為按鈕添加相應(yīng)事件-->
? ? <input type="button" value="改變顏色" onClick="changeColor()" > ?
? ? <input type="button" value="改變寬高" onClick="changeWidthHeight()">
? ? <input type="button" value="隱藏內(nèi)容" onClick="displayNone()">
? ? <input type="button" value="顯示內(nèi)容" onClick="displayBlock()">
? ? <input type="button" value="取消設(shè)置" onClick="changeSet()">
? </form>
? <script type="text/javascript">
//定義"改變顏色"的函數(shù)
? ? function changeColor() {
? ? ? ? var mytxt = document.getElementById('txt');
? ? ? ? mytxt.style.color = 'red';
? ? ? ? mytxt.style.backgroundColor = 'blue';
? ? }
//定義"改變寬高"的函數(shù)
? ? function changeWidthHeight() {
? ? ? ? var mycon = document.getElementById('con');
? ? ? ? mycon.style.width = '300px';
? ? ? ? mycon.style.height = '40px';
? ? ? ? mycon.style.backgroundColor = '#888';
? ? }
//定義"隱藏內(nèi)容"的函數(shù)
? ? function displayNone() {
? ? ? ? document.getElementById('txt').style.display = "none";
? ? }
//定義"顯示內(nèi)容"的函數(shù)
? ? function displayBlock() {
? ? ? ? document.getElementById('txt').style.display = "block";
? ? }
//定義"取消設(shè)置"的函數(shù)
? ? function changeSet() {
? ? ? ? var dlg = confirm("取消設(shè)置");
? ? ? ? if (dlg) {
? ? ? ? ? ? displayBlock();
? ? ? ? } else {
? ? ? ? ? ? displayNone();
? ? ? ? }
? ? }
? </script>
</body>
</html>
查看全部 -
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>className屬性</title>
<style>
? ? body{ font-size:16px;}
? ? .one{
border:1px solid #eee;
width:230px;
height:50px;
background:#ccc;
color:red;
? ? }
.two{
border:1px solid #ccc;
width:230px;
height:50px;
background:#9CF;
color:blue;
}
</style>
</head>
<body>
? ? <p id="p1" > JavaScript使網(wǎng)頁顯示動態(tài)效果并實現(xiàn)與用戶交互功能。</p>
? ? <input type="button" value="添加樣式" onclick="add()"/>
<p id="p2" class="one">JavaScript使網(wǎng)頁顯示動態(tài)效果并實現(xiàn)與用戶交互功能。</p>
? ? <input type="button" value="更改外觀" onclick="modify()"/>
<script type="text/javascript">
? function add(){
? ? ?var p1 = document.getElementById("p1");
? ? ?p1.className = "one";
? }
? function modify(){
? ? ?var p2 = document.getElementById("p2");
? ? ?p2.className = "two";
? }
</script>
</body>
</html>
查看全部 -
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>display</title>
? ? <script type="text/javascript">?
? ? ? ? function hidetext() ?
{ ?
var mychar = document.getElementById("con");
mychar.style.display = "none";
} ?
function showtext() ?
{ ?
var mychar = document.getElementById("con");
? ? ? ? mychar.style.display = "block";
}
? ? </script>?
</head>?
<body> ?
? ? <h1>JavaScript</h1> ?
? ? <p id="con">做為一個Web開發(fā)師來說,如果你想提供漂亮的網(wǎng)頁、令用戶滿意的上網(wǎng)體驗,JavaScript是必不可少的工具。</p>?
? ? <form>
? ? ? ?<input type="button" onclick="hidetext()" value="隱藏內(nèi)容" />?
? ? ? ?<input type="button" onclick="showtext()" value="顯示內(nèi)容" />?
? ? </form>
</body>?
</html>
查看全部 -
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>style樣式</title>
</head>
<body>
? <h2 id="con">I love JavaScript</H2>
? <p id="pcon"> JavaScript使網(wǎng)頁顯示動態(tài)效果并實現(xiàn)與用戶交互功能。</p>
? <script type="text/javascript">
? ? var mychar= document.getElementById("con");
? ? mychar.style.color = 'red';
? ? mychar.style.backgroundColor = '#CCC';?
? ? mychar.style.width = '300px';
? ? mychar.style.height = '40px';
? ? var my_text = document.getElementById('pcon');
? ? my_text.style.fontSize = '50'; //字體大小不起作用?
? ? my_text.style.color = 'blue';
? </script>
</body>
</html>
查看全部 -
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>innerHTML</title>
</head>
<body>
<h2 id="con">javascript</H2>
<p> JavaScript是一種基于對象、事件驅(qū)動的簡單腳本語言,嵌入在HTML文檔中,由瀏覽器負責解釋和執(zhí)行,在網(wǎng)頁上產(chǎn)生動態(tài)的顯示效果并實現(xiàn)與用戶交互功能。</p>
<script type="text/javascript">
? var mychar= document.getElementById('con'); ? ? ? ? ?;
? document.write(" 原標題:"+mychar.innerHTML+"<br>"); //輸出原h(huán)2標簽內(nèi)容
? mychar.innerHTML = "Hello World!";
? document.write("修改后的標題:"+mychar.innerHTML); //輸出修改后h2標簽內(nèi)容
</script>
</body>
</html>
查看全部 -
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>document.getElementById</title>
</head>
<body>
<p id="con">JavaScript</p>
<script type="text/javascript">
? var mychar= document.getElementById('con') ? ? ? ? ?;
? document.write("結(jié)果: "+mychar); //輸出獲取的P標簽。
? // innerText輸出標簽的值,?
? document.write("<br> value: " + mychar.innerText);
? /*
? <br> 表示換行符
? 表示空格?
? ?*/
</script>
</body>
</html>
查看全部 -
<!DOCTYPE html>
<html>
?<head>
? <title> new document </title> ?
? <meta http-equiv="Content-Type" content="text/html; charset=gbk"/> ??
? <script type="text/javascript"> ?
? ??
? ? function openWindow() {
? ? ? ? // 新窗口打開時彈出確認框,是否打開
? ? ? ? var url = prompt("請輸入網(wǎng)址:", 'https://imooc.com');
? ? // 通過輸入對話框,確定打開的網(wǎng)址,默認為 http://idcbgp.cn/
? ??
? ? //打開的窗口要求,寬400像素,高500像素,無菜單欄、無工具欄。
? ? ? ? var is_open = confirm("確認打開嗎?");
? ? ? ? if(is_open) {
? ? ? ? ? ? var w = window.open(url, '_blank', 'width=400,height=500,menubar=no,toolbar=no');
? ? ? ? } else {
? ? ? ? ? ? document.write("No Open!")
? ? ? ? }
? ? }
? ??
? </script>?
?</head>?
?<body>?
?<input type="button" value="新窗口打開網(wǎng)站" onclick="openWindow()" />?
?</body>
</html>
查看全部 -
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>window.open</title>
<script type="text/javascript">
? function Wopen(){
? ? window.open('http://idcbgp.cn', '_blank', 'width=600,height=400,top=100,left=0'); ?
? }?
</script>
</head>
<body>
? ? <input name="button" type="button" onClick="Wopen()" value="點擊我,打開新窗口!" / >
</body>
</html>
查看全部 -
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>prompt</title>
? <script type="text/javascript">
? function rec(){
var score; //score變量,用來存儲用戶輸入的成績值。
score = prompt("請輸入您的成績:");
if(score>=90)
{
? document.write("你很棒!");
}
else if(score>=75)
? ? {
? document.write("不錯吆!");
}
else if(score>=60)
? ? {
? document.write("要加油!");
? ? }
? ? else
{
? ? ? ?document.write("要努力了!");
}
? }
? </script>
</head>
<body>
? ? <input name="button" type="button" onClick="rec()" value="點擊我,對成績做評價!" />
</body>
</html>
查看全部 -
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>document.write</title>
? <script type="text/javascript">
? ? var mystr="我是";
? ? var mychar="JavaScript";
? ? document.write(mychar + "<br>");
? ? document.write(mystr + mychar);
? ? document.write("<br>輸出空格" + " " + "END");
? </script>
</head>
<body>
</body>
</html>
查看全部 -
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>函數(shù)調(diào)用</title>
? ?<script type="text/javascript">
? ? ? ?var num = 8;
? ? ? function contxt() //定義函數(shù)
? ? ? {
? ? ? ? ? num++;
? ? ? ? ? numBody++;
? ? ? ? ?alert("哈哈,調(diào)用函數(shù)了! : " + (numBody + num));
? ? ? }
? ?</script>
</head>
<body>
? ? <script type="text/javascript">
? ? ? ? var numBody = 10;
? ? </script>
? ? <form>
? ? ? <input type="button" ?value="點擊我" onclick="contxt()" /> ?
? ? </form>
</body>
</html>
查看全部
舉報