標籤:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="../javascript/OutSideJs.js"></script>
<style>
.myStyle{
background-color: red;
width: 500px;
height: 500px;
border: 1px solid;
}
</style>
<script>
// alert(width);//輸出資訊
var height = 200;
function test() {
alert(width);
}
function test1() {
alert(height);
}
//document.write 列印指定的常值內容到頁面上
function load(count,str) {
for (var i = 0; i < count; i++) {
document.write("<h1>"+str+"</h1>")
}
//prompt 輸出一個輸入框(讓你輸入東西)
var s= prompt("提示資訊","輸入框的預設資訊");
document.write("<h2>"+s+"</h2>")
}
function cleanValue(obj) {
obj.value="";
}
function getValue(obj) {
var a=obj.value;
if (a!="") {
alert(a.length);//長度
alert(a.substring(0,1));//截取
try{
}catch (e){
e.message;
}finally {
}
//split分割字串
var strs=a.split(",");//分割字串
for (var i=0;i<strs.length;i++){
alert(strs[i]);
}
}
}
function checkUser() {
//document 根據文檔找元素
var name=document.getElementById("name");
var pwd=document.getElementById("pwd");
alert("使用者名稱:"+name.value+"\n"+"密碼:"+pwd.value);
}
//擷取Json資料
var text = ‘{"employees":[‘ +
‘{"firstName":"John","lastName":"Doe" },‘ +
‘{"firstName":"Anna","lastName":"Smith" },‘ +
‘{"firstName":"Peter","lastName":"Jones" }]}‘;
//parse()方法的作用是:解析
function getJson() {
var obj=JSON.parse(text);
alert(obj.employees[0].firstName+"\n"+obj.employees[0].lastName);
}
//擷取日期
function getDate() {
var date=new Date();
alert(date);
}
//對話方塊
function getdel() {
//confirm()彈出確認框(確定 取消)
var s=confirm("您確定要刪除嗎?")
if (s){
alert("刪除成功");
}else {
alert("已取消");
}
}
//定時的函數,每隔一秒就運行一次 setInterval 動作的作用是在播放動畫時,每隔一定的時間就調用函數 或方法 或對象
var id= window.setInterval(function () {
var obj=document.getElementById("time");
obj.value=new Date().getMinutes()+":"+new Date().getSeconds()
},1000);
//設定一個時間 時間到了就執行指定的方法
window.setTimeout(function () {
//clearInterval()方法取消setInterval()方法設定的定時器
window.clearInterval(id);
window.close();
},10000);
//開啟新視窗
function getopenWindow() {
window.open("http://www.baidu.com","我的百度",400,500);
}
//關閉視窗
function getcloseWindow() {
window.close();
}
//擷取螢幕的高度和寬度
function getWindowHeight() {
alert(screen.availHeight);
alert(screen.height);
alert(screen.availWidth);
alert(screen.width);
}
function setCookie() {
var d=new Date();
document.cookie="King"+":"+d.getDay();
var expires = "expires="+d.toGMTString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
function getCookie() {
var str=document.cookie;
alert(str);
}
function getBackgroundColor() {
var div1=document.getElementById("div1");
// div1.style.backgroundColor="red";
div1.className="myStyle";//???????不好使
// div1.innerText="King&Queen" //插入內部文本
div1.innerHTML="<h1>HTML</h1>"//插入HTML
}
//增加行數
function addrow() {
var tab1=document.getElementById("tab1")
var row=tab1.insertRow();
var c1=row.insertCell(0);
var c2=row.insertCell(1);
var c3=row.insertCell(2);
c1.innerText="a";
c2.innerText="b";
c3.innerText="c";
}
//擷取X Y的座標
function getXY(event) {
alert(event.clientX+":"+event.clientY);
}
//刪除元素
function deleteTable() {
var tab1=document.getElementById("tab1");
document.body.removeChild(tab1);
}
//下拉式清單
function getItem(obj) {
alert(obj.value);
}
//跳轉畫面
function go() {
// 第一種方法 window.location.href="邊走邊喬.html";
window.open("邊走邊喬.html","","0,0,1200,1200")
}
</script>
</head>
<body>
<h1 ondblclick="load(10,‘hello King‘)">點擊我</h1>
<input type="text" onfocus="cleanValue(this)" onblur="getValue(this)" value="我是輸入框">
<input type="text" id="time">
使用者名稱:<input type="text" id="name">
密碼:<input type="password" id="pwd">
<input type="button" value="測試" onclick="checkUser()">
<input type="button" value="Json" onclick="getJson()">
<input type="button" value="擷取日期" onclick="getDate()">
<input type="button" value="對話方塊" onclick="getdel()">
<input type="button" value="開啟新視窗" onclick="getopenWindow()">
<input type="button" value="關閉新視窗" onclick="getcloseWindow()">
<input type="button" value="螢幕的尺寸" onclick="getWindowHeight()">
<input type="button" value="設定Cookie" onclick="setCookie()">
<input type="button" value="讀取Cookie" onclick="getCookie()">
<input type="button" value="改變DIV背景色" onclick="getBackgroundColor()">
<input type="button" value="增加行數" onclick="addrow()">
<input type="button" value="刪除table" onclick="deleteTable()">
<input type="button" value="跳轉" onclick="go()">
<div id="div1" style="width: 200px;height: 200px;"></div>
<table id="tab1" style="border: 1px solid red;width: 300px;">
<tr style="border: 1px solid black">
<td width="100px">1</td>
<td width="100px">2</td>
<td width="100px">3</td>
</tr>
<tr>
<td width="100px">1</td>
<td width="100px">2</td>
<td width="100px">3</td>
</tr>
<tr>
<td width="100px">1</td>
<td width="100px">2</td>
<td width="100px">3</td>
</tr>
</table>
<select style="width: 100px;height: auto" onchange="getItem(this)">
<option value="0">--請選擇--</option>
<option value="1">江西省</option>
<option value="2">山東省</option>
<option value="3">福建省</option>
</select>
</body>
</html>
JS的一些簡單一實例用法