魔鬼字典 JavaScript 筆記 代碼比較多亂第2/3頁(yè)
更新時(shí)間:2010年03月09日 21:03:36 作者:
魔鬼字典 JavaScript 筆記 代碼比較多亂,對(duì)于有經(jīng)驗(yàn)的看容易點(diǎn)。
/例子/ /原型方法如果如果自己重新定義了一遍,再改變基類的原型方法后就不隨著它的改變而改變了/
『
function Person(name,sex) { //Person類的構(gòu)造函數(shù)
this.name = name;
this.sex = sex;
}
Person.prototype.age = 12; //為Person類的prototype屬性對(duì)應(yīng)的prototype對(duì)象的屬性賦值,
//相當(dāng)于為Person類的父類添加屬性
Person.prototype.print = function() { //為Person類的父類添加方法
alert(this.name+"_"+this.sex+"_"+this.age);
};
var p1 = new Person("name1","male"); //p1的age屬性繼承子Person類的父類(即prototype對(duì)象)
var p2 = new Person("name2","male");
p1.print(); //name1_male_12
p2.print(); //name2_male_12
p1.age = 34; //改變p1實(shí)例的age屬性
p1.print(); //name1_male_34
p2.print(); //name2_male_12
Person.prototype.age = 22; //改變Person類的超類的age屬性
p1.print(); //name1_male_34(p1的age屬性并沒(méi)有隨著prototype屬性的改變而改變)
p2.print(); //name2_male_22(p2的age屬性發(fā)生了改變)
p1.print = function() { //改變p1對(duì)象的print方法
alert("i am p1");
}
p1.print(); //i am p1(p1的方法發(fā)生了改變)
p2.print(); //name2_male_22(p2的方法并沒(méi)有改變)
Person.prototype.print = function() { //改變Person超類的print方法
alert("new print method!");
}
p1.print(); //i am p1(p1的print方法仍舊是自己的方法)
p2.print(); //new print method!(p2的print方法隨著超類方法的改變而改變)
』
JS高級(jí)編程 第四章 BOM ↑↓←→
BOM(瀏覽器對(duì)象模型)
瀏覽器對(duì)象分為
{
1、Window窗口對(duì)象(包含整個(gè)瀏覽器)
2、Location地址對(duì)象(主要包含地址欄)
3、Document文檔對(duì)象(用Document.write()輸出時(shí)顯示的地方,占瀏覽器的大部分空間)
4、Form表單對(duì)象(包含在文檔對(duì)象中,由表單組成)
}
//window對(duì)象(Window對(duì)象表示整個(gè)瀏覽器窗口,但不必表示其中包含的內(nèi)容,主要用于調(diào)整瀏覽器的大?。?
{
window對(duì)象表示整個(gè)瀏覽器窗口
moveBy(dx,dy)——把瀏覽器窗口相對(duì)當(dāng)前位置水平移動(dòng)dx個(gè)像素,垂直移動(dòng)dy個(gè)像素。dx值為負(fù)數(shù),向左移動(dòng)窗口,dy值為負(fù)數(shù),向上移動(dòng)窗口。
moveTo(x,y)——移動(dòng)瀏覽器窗口,使它的左上角位于用戶屏幕的(x,y)處??梢允褂秘?fù)數(shù),不過(guò)這樣會(huì)把部分窗口移出屏幕的可視區(qū)域。
function click1() {
window.moveTo(Math.random()*1000,Math.random()*1000);
}
resizeBy(dw,dh)——相對(duì)于瀏覽器窗口的當(dāng)前大小,把它口的寬度調(diào)整dw個(gè)像素,高度調(diào)整dy個(gè)像素。dw為負(fù)數(shù),把縮小窗口的寬度,dy為負(fù)數(shù),縮小窗口的高度。
resizeTo(w,h)——把窗口的寬度調(diào)整為w,高度調(diào)整為h。不能使用負(fù)數(shù)。
窗口對(duì)象的方法主要用來(lái)提供信息或輸入數(shù)據(jù)以及創(chuàng)建一個(gè)新的窗口?! ?chuàng)建一個(gè)新窗口open()使用window.open(參數(shù)表)方法可以創(chuàng)建一個(gè)新的窗口。其中參數(shù)表提供有窗口的主要特性和文檔及窗口的命名。
open(”打開(kāi)窗口的url”,”窗口名”,”窗口特征”)
窗口的特征如下,可以任意組合:
top=# 窗口頂部離開(kāi)屏幕頂部的像素?cái)?shù)
left=# 窗口左端離開(kāi)屏幕左端的像素?cái)?shù)
width=# 窗口的寬度
height=# 窗口的高度
menubar=... 窗口有沒(méi)有菜單,取值yes或no
toolbar=... 窗口有沒(méi)有工具條,取值yes或no
location=... 窗口有沒(méi)有地址欄,取值yes或no
directories=... 窗口有沒(méi)有連接區(qū),取值yes或no
scrollbars=... 窗口有沒(méi)有滾動(dòng)條,取值yes或no
status=... 窗口有沒(méi)有狀態(tài)欄,取值yes或no
resizable=... 窗口給不給調(diào)整大小,取值yes或no
}
//對(duì)話框
{
alert (“m提示信息") //顯示包含消息的對(duì)話框。
alert("第一種對(duì)話框"); //彈出一個(gè)警告框
confirm(“提示信息”) //顯示一個(gè)確認(rèn)對(duì)話框,包含一個(gè)確定取消按鈕 //confirm : 確定; 使鞏固; 批準(zhǔn)
if(confirm("Are you sure?")) ()中寫(xiě)的是提示信息,此方法會(huì)返回一個(gè)Boolean值,點(diǎn)擊確定返回TRUE,點(diǎn)擊取消返回FALSE
{
alert("你點(diǎn)的yes"); //通常用法,當(dāng)你點(diǎn)擊確定時(shí)顯示的信息
}
else
{
alert("你點(diǎn)的no"); //當(dāng)你點(diǎn)擊取消時(shí)顯示的信息
}
Prompt(”提示信息“) //彈出提示信息框
open ("url","name") //打開(kāi)具有指定名稱的新窗口,并加載給定 URL 所指定的文檔;如果沒(méi)有提供 URL,則打開(kāi)一個(gè)空白文檔,再加一個(gè)參數(shù)就是描述(打開(kāi)的瀏覽器的寬、高....)
close ( ) //關(guān)閉當(dāng)前窗口
/例子/
<script type="text/javascript" language="javascript">
var win;
function openwindow() {
win = open("http://www.sina.com","新浪","height=150,width=300,top=10,left=10,resizable=yes");
}
function closewindow() {
win.close();
}
</script>
</head>
<body>
<input type="text" id="txtname"/>
<script language="javascript" type="text/javascript">
文本框中的默認(rèn)值
↓
document.getElementById("txtname").value = prompt("請(qǐng)輸入你的名字","qw");
↑
腳本提示的文本
</script>
<input type="button" value="打開(kāi)一個(gè)新的窗口" onclick="openwindow()"/>
<input type="button" value="關(guān)閉剛才打開(kāi)的窗口" onclick="closewindow()"/>
</body>
}
defaultStatus status :設(shè)置瀏覽器最下端的狀態(tài)欄,defaultStatus是持久性的,status是臨時(shí)性的,注意:IE7默認(rèn)情況下是將“允許狀態(tài)欄通過(guò)腳本更新”設(shè)置為禁用的,可以在INternet選項(xiàng)-》安全性里修改此設(shè)置
『
<script language="javascript" type="text/javascript">
function mystatus()
{
//window.defaultStatus = "你有沒(méi)有注意我的存在??";
window.status = "你有沒(méi)有注意我的存在?";
}
</script>
</head>
<body onload="mystatus()">
<a onmouseover="window.status='你有沒(méi)有注意我的變化??';return true" onmouseout="window.status='你有沒(méi)有注意我的存在??';return true">www.baidu.com</a>
↑ ↑
注意用的是單引 分號(hào)和return true不能省略,return true可以防止地址出現(xiàn)在狀態(tài)欄中
</body>
』
history
『
<head>
<title>無(wú)標(biāo)題頁(yè)</title>
<script language="javascript" type="text/javascript">
function go_destination()
{
history.go(-1); //history.go(-1)和history.back()效果是一樣的
}
function go_back()
{
history.back();
}
</script>
</head>
<body>
<input type="button" onclick="go_destination()" value="上一頁(yè)" />
<input type="button" onclick="go_back()" value="上一頁(yè)" />
</body>
Back ( ) 方法相當(dāng)于后退按鈕
forward ( ) 方法相當(dāng)于前進(jìn)按鈕
go (1)代表前進(jìn)1頁(yè),等價(jià)于forward( )方法;
go(-1) 代表后退1頁(yè),等價(jià)于back( )方法;
』
Document對(duì)象
『
alinkColor 設(shè)置或檢索文檔中所有活動(dòng)鏈接的顏色
bgColor 設(shè)置或檢索 Document 對(duì)象的背景色
body 指定文檔正文的開(kāi)始和結(jié)束
linkColor 設(shè)置或檢索文檔鏈接的顏色
location 包含關(guān)于當(dāng)前 URL 的信息
title 包含文檔的標(biāo)題
url 設(shè)置或檢索當(dāng)前文檔的 URL
vlinkColor 設(shè)置或檢索用戶訪問(wèn)過(guò)的鏈接的顏色
<head>
<title>無(wú)標(biāo)題頁(yè)</title>
<script language="javascript" type="text/javascript">
function showTitle()
{
document.title="我不做大哥好多年"; //設(shè)置標(biāo)題
}
function change(color)
{
document.bgColor=color;
}
</script>
</head>
<body>
<input type="button" onclick="showTitle()" value="點(diǎn)擊我顯示標(biāo)題" />
<h2>移過(guò)來(lái)我變色show給你</h2>
<span onmouseover="change('#CCCCCC')">變紅色</span>
<span onmouseover="change('blue')">變紅色</span>
<span onmouseover="change('yellow')">變紅色</span>
<img src="../js02/img/3.jpg"alt="" height="200" width="200" onmouseover="alert(document.images[0].src)" />
<img src="../js02/img/4.jpg"alt="" height="200" width="200" onmouseover="alert(document.images[1].src)" />
</body>
cookie屬性
這里主要介紹一下cookie屬性。cookie是當(dāng)你瀏覽某網(wǎng)站時(shí),網(wǎng)站存儲(chǔ)在你機(jī)器上的一個(gè)小文本文件,它記錄了你的用戶ID,密碼、瀏覽過(guò)的網(wǎng)頁(yè)、停留的時(shí)間等信息,當(dāng)你再次來(lái)到該網(wǎng)站時(shí),網(wǎng)站通過(guò)讀取cookie,得知你的相關(guān)信息,就可以做出相應(yīng)的動(dòng)作,如在頁(yè)面顯示歡迎你的標(biāo)語(yǔ),或者讓你不用輸入ID、密碼就直接登錄等等。
cookie屬性也包含了自己的一些屬性值:
expires:用來(lái)設(shè)置cookie的過(guò)期時(shí)間。
domain:用來(lái)設(shè)置cookie的起作用的網(wǎng)站域名范圍。
path:用來(lái)設(shè)置cookie起作用的網(wǎng)站路徑。
secure:如果設(shè)置了這個(gè)屬性,cookie的值只能在安全設(shè)置很高的環(huán)境下才能被讀取。
cookie的目的就是為用戶帶來(lái)方便,為網(wǎng)站帶來(lái)增值。cookie屬性也包含了自己的一些屬性值:
我們可以通過(guò)document.cookie操作來(lái)設(shè)置某個(gè)cookie值。
』
Location對(duì)象
『
屬性
host 設(shè)置或檢索位置或 URL 的主機(jī)名和端口號(hào)
hostname 設(shè)置或檢索位置或 URL 的主機(jī)名部分
href 設(shè)置或檢索完整的 URL 字符串
方法
assign("url") 加載 URL 指定的新的 HTML 文檔。
reload() 重新加載當(dāng)前頁(yè)
replace("url") 通過(guò)加載 URL 指定的文檔來(lái)替換當(dāng)前文檔
<head>
<title>無(wú)標(biāo)題頁(yè)</title>
<script language="javascript" type="text/javascript">
function jump()
{
location.href=document.myform.menu.value;
}
function jump1()
{
location.replace(document.myform.menu.value); //注意replace中放的是字符串給用"" //replace() 不記錄歷史即點(diǎn)完后不能后退
}
function jump2() {
location.assign(document.myform.menu.value); //記錄歷史,可以后退
}
function showHost()
{
alert(location.host);
}
function showHostName()
{
alert(location.hostname);
}
function reLoad() {
location.reload();
}
</script>
</head>
<body>
<form name="myform" action="">
<select name="menu" onchange="jump2()"><!--注意jump1的區(qū)別--> //<select> :下拉菜單 <option>每一個(gè)選項(xiàng)
<option >-請(qǐng)選擇-</option>
<option value="test1.htm">犬夜叉</option>
<option value="test2.htm">殺生丸</option>
</select>
<input type="button" value="顯示主機(jī)名和端口號(hào)" onclick="showHost()" />
<input type="button" value="顯示主機(jī)名" onclick="showHostName()" />
<!--<input />-->
<input type="button" value="刷新" onclick="reLoad(true)" /><!--true表示從服務(wù)端加載false表示從緩存加載-->
</form>
</body>
』
Navigator //Navigator 對(duì)象是由 JavaScript runtime engine 自動(dòng)創(chuàng)建的,包含有關(guān)客戶機(jī)瀏覽器的信息(基本不用)
『
document.write(navigator.appCodeName, "<br />"); //與瀏覽器相關(guān)的內(nèi)部代碼名
document.write(navigator.appName, "<br />"); //瀏覽器的正式名稱
document.write(navigator.appVersion, "<br />"); //瀏覽器的版本號(hào)
document.write(navigator.browserLanguage,"<br />");//FireFox不支持
document.write(navigator.cpuClass, "<br />"); //FireFox不支持,返回用戶計(jì)算機(jī)的cpu的型號(hào),通常intel芯片返回"x86"
document.write(navigator.language,"<br />");//IE不支持
document.write(navigator.platform, "<br />"); // 瀏覽器正在運(yùn)行的操作系統(tǒng)平臺(tái)
document.write(clientInformation.appName);//微軟特有的對(duì)象、
/顯示/
Mozilla
Microsoft Internet Explorer
4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)
zh-cn
x86
undefined
Win32
Microsoft Internet Explorer
』
//screen對(duì)象
『
availHeight——窗口可以使用的屏幕的高度(以像素計(jì)),其中包括操作系統(tǒng)元素(如Windows工具欄)需要的空間。
availWidth——窗口可以使用的屏幕的寬度(以像素計(jì))。
colorDepth——用戶表示顏色的位數(shù)。大多數(shù)系統(tǒng)采用32位。
height——屏幕的高度,以像素計(jì)。
width——屏幕的寬度,以像素計(jì)
window.moveTo(0,0); //將瀏覽器調(diào)整到最左上角
window.resizeTo(screen.availWidth,screen.availHeight,"<br />"); //將屏幕設(shè)置到最大化
document.write(screen.availWidth + " " + screen.availHeight, "<br />"); //輸出顯示屏幕的可用最大寬和高 “開(kāi)始狀態(tài)欄占一部分”
document.write(screen.height + " " + screen.width, "<br />"); //屏幕的最大高和寬
document.write(" " + screen.colorDepth, "<br />");
』
//打開(kāi)一個(gè)新的窗體
『
<SCRIPT language="JavaScript" >
function openwindow( ) {
var newWindow = window.open("about:blank", "newWindow", "toolbars=yes, scrollbars=yes, location=yes,statusbars=yes,menubars=yes,resizable=yes,width=650,height=150"); //()里面的東西給寫(xiě)對(duì)了否則讀不出來(lái)實(shí)在不行用“"","",""”也行
newWindow.document.open(); //流體打開(kāi)
newWindow.document.write("<html><head><title>極品名茶--鐵觀音</title></head>");
newWindow.document.write("<body><h1>先生您好,來(lái)點(diǎn)鐵觀音嘗嘗吧?<a href=''>來(lái)點(diǎn)</a> <a href=''>不了</a></h1></body></html>");
newWindow.document.close(); //流體關(guān)閉
}
</SCRIPT>
</head>
<body onload="openwindow()">
』
//跨窗體傳值↑↓←→
『
主窗體
『
<title>無(wú)標(biāo)題頁(yè)</title>
<script type="text/javascript" language="javascript">
function aa()
{
open("a2.htm","aaa","");
}
</script>
</head>
<body>
<form name="f1" action="" method="post">
↑
是name不是id,用id會(huì)有錯(cuò)誤
<input type="text" name="t1"/>
<button onclick="aa()">sdf</button>
</form>
</body>
』
副窗體(a2.htm)
『
<title>無(wú)標(biāo)題頁(yè)</title>
<script type="text/javascript" language="javascript">
function bb()
{
document.f1.t2.value=window.opener.document.f1.t1.value;
↑
f1后面的東西全部點(diǎn)不出來(lái),都給硬打
}
</script>
</head>
<body onload="bb()">
<form name="f1" action="" method="post">
<input type="text" name="t2"/>
</form>
</body>
』
』
JS 第五章 DOM(文本對(duì)象模型) ↑↓←→
//節(jié)點(diǎn)類型
『
1、元素節(jié)點(diǎn)
2、文本節(jié)點(diǎn)
<p>段落</p> //<p>為元素節(jié)點(diǎn),“段落”為文本節(jié)點(diǎn)
』
//節(jié)點(diǎn)的三個(gè)屬性
『
nodeName節(jié)點(diǎn)元素名稱
nodeType節(jié)點(diǎn)元素類型(1-12)
nodeValue節(jié)點(diǎn)元素的值
1 element_node 2 attribute_node 3 text_node
4 cdata_section_node 5 entity_reference_node 6 entity_node
7 processing_instruction_node 8 comment_node 9 document_code
10 document_type_node 11 document_fragment_node 12 notation_node
『例子』
『
<a id="sirius" href="">Journey to the stars</a>
-------------------------------------------------
外置JS腳本:
addLoadListener(init);
function init()
{
var elementRef = document.getElementById("sirius");
elementRef.firstChild.nodeValue = "sdfsd"; //將“Journey to the stars”動(dòng)態(tài)的改變?yōu)椤皊dfsd” ; 注意要加上“firstChild”
alert("The element is:" + elementRef.nodeName); //顯示:A
alert("The element is:" + elementRef.nodeType); //顯示: 1
alert("The element is:" + elementRef.firstChild.nodeValue); //顯示:sdfsd
return true;
}
function addLoadListener(fn)//注冊(cè)事件函數(shù)
{
if(typeof window.addEventListener!="undefined")
{
window.addEventListener("load",fn,false);
}
else if(typeof document.addEventListener!="undefined")
{
document.addEventListener("load",fn,false);
}
else if(typeof window.attchEvent!="undefined")
{
document.attchEvent("onload",fn);
}
else
{
var oldfn=window.onload;
if(typeof window.onload!="function")
{
window.onload=fn;
}
else
{
window.onload=function()
{
oldfn();
fn();
};
}
}
}
』
』
訪問(wèn)DOM中的元素
『
getElementById可以通過(guò)唯一的ID屬性值 。
getElementById很適合用于查找一個(gè)單獨(dú)的元素
<a id="sirius" href="sirius.html">Journey to the stars</a>
var elementRef = document.getElementById("sirius");
『例子』
復(fù)制代碼 代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>無(wú)標(biāo)題頁(yè)</title>
<script language="javascript" type="text/javascript" src="javascript/GetElementByName.js">
</script>
<link rel="Stylesheet" type="text/css" href="css/GetElementByName.css" />
</head>
<body>
<h1>Accessing elements</h1>
<ul>
<li><a href="test1.htm" name="myLink">Sirius</a></li>
<li><a href="test2.htm" name="myLink">Canopus</a></li>
<li><a href="test1.htm" name="myLink">Arcturus</a></li>
<li><a href="test2.htm" name="myLink">Vega</a></li>
</ul>
</body>
</html>
--------------------------------------------------------
外置JS腳本的內(nèi)容:
function init()
{
var anchors=document.getElementsByName("myLink");
for(var i=0;i<anchors.length;i++)
{
anchors[i].className="starLink";
}
//return true;
}
『例子』
『
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>無(wú)標(biāo)題頁(yè)</title>
<script language="javascript" type="text/javascript">
function myCopy() {
var textBox1=document.getElementById("txt1");
var textBox2=document.getElementById("txt2");
textBox2.value=textBox1.value;
↑
兩個(gè)文本框中的文本相同
}
</script>
</head>
<body>
<form id="myForm" action="" method="post">
<input id="txt1" />
<input type="button" value=">>>" onclick="myCopy()" /> //如果onclick()函數(shù)要是想調(diào)用兩個(gè)以上的事件就在兩個(gè)事件中間用“;”隔開(kāi)
↑
button上顯示的字
<input id="txt2" />
</form>
</body>
</html>
』
JavaScript提供了通過(guò)標(biāo)簽名字來(lái)返回一組元素的方法:getElementsByTagName。
<a href="sirius.html">Sirius</a>
<a href="canopus .htm">canopus </a>
<a href="arcturus .html">arcturus </a>
<a href=“vega .html”>vega </a>
var anchorArray = document.getElementsByTagName("a");
『例子』
『
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>無(wú)標(biāo)題頁(yè)</title>
<script language="javascript" type="text/javascript">
function myCopy()
{
var myArray = document.getElementsByTagName("input");//返回元素集合,()中放的是標(biāo)記的名字,記住用“”
var h2 = document.getElementsByTagName("h2");
for(var i=0;i<myArray.length;i++)
{
if (myArray[i].type == "text") {
myArray[i].value=h2[0].innerHTML; //"innerHTML"表示的是<h2></h2>中間包含的文本,點(diǎn)不出來(lái)只能硬打
}
}
}
</script>
</head>
<body>
<form action="" method="post" id="myForm">
<h2 id="h2">getElementsByTagName</h2><br />
<input name="txt1" />
<input name="txt1" />
<input name="txt1" />
<input id="btn" type="button" value="Click Me" onclick="myCopy()" />
</form>
</body>
</html>
』
』
//節(jié)點(diǎn)的訪問(wèn)順序
『
node.childNodes 指定節(jié)點(diǎn)的所有子節(jié)點(diǎn),包括文本節(jié)點(diǎn)和所有其他元素 //用法: 節(jié)點(diǎn).childNodes 注意:打完不用加()
node.firstChild 指定節(jié)點(diǎn)的第一個(gè)子節(jié)點(diǎn)
node.lastChild 指定節(jié)點(diǎn)的最后一個(gè)子節(jié)點(diǎn);
node.parentNode 指定節(jié)點(diǎn)的上級(jí)節(jié)點(diǎn);
node.nextSibling 指定節(jié)點(diǎn)的下一個(gè)兄弟節(jié)點(diǎn);
node.previousSibling 指定節(jié)點(diǎn)的上一個(gè)兄弟節(jié)點(diǎn)
『例子』
『
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>無(wú)標(biāo)題頁(yè)</title>
<script type="text/javascript" language="javascript">
function myDominspector()
{
var DOMString="";
var DOMText="";
var demolist=document.getElementById("eventsList");
if(demolist==null) //判斷:獲得的集合是不是為空,或者是否成功獲取
{
return;
}
if(demolist.hasChildNodes()) //hasChildNodes() 該方法能判斷是否有子節(jié)點(diǎn)
{
var ch=demolist.childNodes; //如果有子節(jié)點(diǎn)用“ch”獲得該集合 //絕大部分都給硬打,特別注意大小寫(xiě)
for(var i=0;i<ch.length;i++)
{
DOMString+=ch[i].nodeName+"\n"; //獲得子節(jié)點(diǎn)的標(biāo)記名字
DOMText+=ch[i].firstChild.nodeValue; //獲得子節(jié)點(diǎn)的值,注意給加"firstChild"
}
document.write(DOMString);
document.write(DOMText);
document.write(ch[2].childNodes[0].firstChild.nodeValue); //因?yàn)椤邦惐眄?xiàng)三”用的是嵌套標(biāo)記所以用上面的方法獲取的是“null”
}
}
</script>
</head>
<body>
<h1>一級(jí)標(biāo)題</h1>
<p>段落</p>
<h2>二級(jí)標(biāo)題</h2>
<ul id="eventsList">
<li>列表項(xiàng)一</li>
<li>列表項(xiàng)二</li>
<li><a >列表項(xiàng)三</a></li>
<li>列表項(xiàng)四</li>
</ul>
<p>段落</p>
<p>段落</p>
<input type="button" onclick="myDominspector()" value="Click Me"/>
</body>
</html>
『顯示』
點(diǎn)擊按鈕后
LI LI LI LI 列表項(xiàng)一 列表項(xiàng)二 null列表項(xiàng)四 列表項(xiàng)三
』
』
//空白節(jié)點(diǎn)
『
對(duì)于一些文本描述的DOM結(jié)構(gòu)(例如HTML文件),一些瀏覽器會(huì)在元素節(jié)點(diǎn)之間插入一些空白節(jié)點(diǎn)。
對(duì)于IE,忽略所有空白節(jié)點(diǎn)
『例子』
『
/////////外部js腳本:
addLoadListener(init); //在加載時(shí)就運(yùn)行此腳本
function init() {
var star2 = document.getElementById("star1").nextSibling; //得到"star1"的下一個(gè)兄弟節(jié)點(diǎn)--star2(想的是這樣,在IE中能實(shí)現(xiàn),但是在火狐中,會(huì)加上相應(yīng)的空白節(jié)點(diǎn),此處在火狐中獲得的是空白節(jié)點(diǎn))
//alert(star2.nodeName);
if(star2.nodeType=="3")//3表示文本節(jié)點(diǎn)(text_node),空白節(jié)點(diǎn)也是文本節(jié)點(diǎn),要是不是空白節(jié)點(diǎn)此處的nodeType為1(element_node)
{
star2=star2.nextSibling;
}
star2.className = "starLink";
var temp = document.documentElement.firstChild.nextSibling.firstChild; //documentElement表示HTML元素
alert(temp.nodeName);
if (temp.nodeType == "3") {
temp = temp.nextSibling;
}alert(temp.nodeName);
return true; //加上這句就是在網(wǎng)頁(yè)上即使有錯(cuò)誤也不會(huì)報(bào)錯(cuò),可以繼續(xù)執(zhí)行下面的代碼
}
function addLoadListener(fn) //此函數(shù)是驗(yàn)證瀏覽器的版本是IE還是火狐還是別的什么,可以直接拷貝過(guò)來(lái)用
{
if(typeof window.addEventListener!="undefined")
{
window.addEventListener("load",fn,false);
}
else if(typeof document.addEventListener!="undefined")
{
document.addEventListener("load",fn,false);
}
else if(typeof window.attchEvent!="undefined")
{
window.attchEvent("load",fn);
}
else
{
var oldfn=window.onload;
if(typeof window.onload!="function")
{
window.onload=fn;
}
else
{
window.onload=function()
{
oldfn();
fn();
};
}
}
}
----------------------------------------------------
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>無(wú)標(biāo)題頁(yè)</title>
<script language="javascript" type="text/javascript" src="javascript/BlankNodes.js">
</script>
<link rel="Stylesheet" type="text/css" href="css/BlankNoses.css" />
</head>
<body>
<h1>Accessing elements</h1>
<div id="outerGalaxy">
<ul id="starList">
<li id="star1">Rigel</li>
<li id="star2">Altair</li>
<li id="star3">Betelgeuse</li>
</ul>
</div>
</body>
</html>
』
』
//創(chuàng)建元素節(jié)點(diǎn)和文本節(jié)點(diǎn) 創(chuàng)建方法的選擇
『
createElements就是創(chuàng)建新元素的函數(shù)。
var newAnchor = document.createElement("a");
創(chuàng)建函數(shù)createTextNode創(chuàng)建文本節(jié)點(diǎn)
var anchorText = document.createTextNode("monoceros");
有3種基本的方法來(lái)將新的元素節(jié)點(diǎn)或者文本節(jié)點(diǎn)插入到網(wǎng)頁(yè)中
放到最后:appendChild ()方法 //用法: 前一個(gè)節(jié)點(diǎn).appendChild(后一個(gè)節(jié)點(diǎn)的名字) 注意:名字不用“”擴(kuò)起 ; 這種方法是將節(jié)點(diǎn)加到“前一個(gè)節(jié)點(diǎn)”的后面
放到某節(jié)點(diǎn)之前:insertBefore ()方法 //用法 : 父節(jié)點(diǎn).insertBefore(新的節(jié)點(diǎn),已定義好的節(jié)點(diǎn))→→(顯示的順序):新的節(jié)點(diǎn) 已定義好的節(jié)點(diǎn)
替換掉原有的某個(gè)節(jié)點(diǎn): replaceChild ()方法 //用法 : 父節(jié)點(diǎn).replaceChild(新的節(jié)點(diǎn),舊的節(jié)點(diǎn))→→→(顯示結(jié)果):新的節(jié)點(diǎn)替換掉舊的節(jié)點(diǎn)
『例子』
『
下面是外置JS腳本的內(nèi)容:
addLoadListener(init);
function init()
{
var anchorText=document.createTextNode("monoceros"); //創(chuàng)建一個(gè)文本節(jié)點(diǎn)
var newAnchor = document.createElement("a"); //創(chuàng)建一個(gè)元素節(jié)點(diǎn)
newAnchor.;//給新建的超鏈接節(jié)點(diǎn)添加鏈接屬性
newAnchor.appendChild(anchorText); //元素節(jié)點(diǎn)和文本節(jié)點(diǎn)進(jìn)行連接
var parent=document.getElementById("starLinks");
var newChild=parent.appendChild(newAnchor); //把新建的節(jié)點(diǎn)添加到原有超級(jí)連接的后面
return true;
}
function addLoadListener(fn) //下面是判斷瀏覽器的類型
{
if (typeof window.addEventListener != 'undefined')
{
window.addEventListener('load', fn, false);
}
else if (typeof document.addEventListener != 'undefined')
{
document.addEventListener('load', fn, false);
}
else if (typeof window.attachEvent != 'undefined')
{
window.attachEvent('onload', fn);
}
else
{
var oldfn = window.onload;
if (typeof window.onload != 'function')
{
window.onload = fn;
}
else
{
window.onload = function()
{
oldfn();
fn();
};
}
}
}
-----------------------------------------------------------
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>無(wú)標(biāo)題頁(yè)</title>
<script language="javascript" type="text/javascript" src="javascript/CreateElement1.js"></script>
</head>
<body>
<h1>Creating elements and test nodes</h1>
<p id="starLinks">
<a href="test1.htm">Sirius</a>
</p>
</body>
</html>
』
』
//改變?cè)氐念愋?
『
沒(méi)有什么直接的、簡(jiǎn)單的方法來(lái)改變一個(gè)元素的類型。
改變?cè)仡愋偷闹饕侄巍寺?
注意:更改DOM的節(jié)點(diǎn)結(jié)構(gòu)時(shí)要當(dāng)心
『例子』
『
下面是外置JS腳本的內(nèi)容:
addLoadListener(init);
function init()
{
var div=document.createElement("div");
var p=document.getElementById("starLinks");
for(var i=0;i<p.childNodes.length;i++)
{
var clone=p.childNodes[i].cloneNode(true);//true表示克隆子節(jié)點(diǎn)本身
div.appendChild(clone);
}
//debugger;
div.id=p.getAttribute("id");
div.className="starLink";
p.parentNode.replaceChild(div,p);
}
function addLoadListener(fn)
{
if (typeof window.addEventListener != 'undefined')
{
window.addEventListener('load', fn, false);
}
else if (typeof document.addEventListener != 'undefined')
{
document.addEventListener('load', fn, false);
}
else if (typeof window.attachEvent != 'undefined')
{
window.attachEvent('onload', fn);
}
else
{
var oldfn = window.onload;
if (typeof window.onload != 'function')
{
window.onload = fn;
}
else
{
window.onload = function()
{
oldfn();
fn();
};
}
}
}
-----------------------------------------------------------
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>無(wú)標(biāo)題頁(yè)</title>
<script language="javascript" type="text/javascript" src="javascript/ChangeTypeOfElement1.js"></script>
<link rel="Stylesheet" type="text/css" href="css/BlankNoses.css" />
</head>
<body>
<h1>Changing the type of an element</h1>
<p id="starLinks">
<a href="test1.htm">Sirius</a>
<a href="test2.htm">Achanar</a>
</p>
</body>
</html>
』
』
//刪除一個(gè)元素或文本節(jié)點(diǎn)
『
removeChild函數(shù)能夠用于刪除父節(jié)點(diǎn)的任意子節(jié)點(diǎn),并返回被刪除的對(duì)象。
刪除的元素不再存在于DOM中:它只存在于內(nèi)存中
『例子1』
『
var anchor=document.getElementById("sirius"); //得到要?jiǎng)h除的節(jié)點(diǎn)對(duì)象
var parent=anchor.parentNode; //找到它的父節(jié)點(diǎn)
var removedChild=parent.removeChild(anchor); //用法 : 父節(jié)點(diǎn).removeChild(子節(jié)點(diǎn)對(duì)象);
』
『例子2』
『
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>無(wú)標(biāo)題頁(yè)</title>
<link rel="Stylesheet" type="text/css" href="css/BlankNoses.css" />
<script language="javascript" type="text/javascript" src="javascript/RemoveElement2.js"></script>
</head>
<body>
<h1>Removing an element</h1>
<div id="starContainer">
<p id="starLinks" class="starLink">
<a href="test1.htm">Aldebaran</a>
<a href="test2.htm">Castor</a>
</p>
</div>
</body>
</html>
----------------------------------------------------------------------------
下面是外掛式j(luò)s腳本內(nèi)容:
var parent=document.getElementById("starLinks"); //得到父節(jié)點(diǎn)的對(duì)象(f2)
var container=document.getElementById("starContainer"); //得到父節(jié)點(diǎn)的父節(jié)點(diǎn)的對(duì)象(f1)
while(parent.childNodes.length>0)
{
container.insertBefore(parent.childNodes[0],parent); //將F2中的子節(jié)點(diǎn)和F1對(duì)調(diào)
}
container.removeChild(parent); //對(duì)調(diào)結(jié)束后刪除F1
』
』
//讀寫(xiě)元素屬性
『
HTML元素最為常用的部分就是它的屬性,JavaScript不僅能夠讀取這些屬性值,而且還能寫(xiě)回新值。
getAttribute可以用于讀取一個(gè)屬性的值,而setAttribute則可以用于寫(xiě)入新值
『例子1』
『
<a id="antares" href="test1.htm" title="Marssion">Antares</a>
---------------------------------------------------------------
下面是外置JS腳本的內(nèi)容:
var anchor=document.getElementById("antares"); //獲得要獲得屬性的那個(gè)對(duì)象
var anchorId=anchor.getAttribute("id"); //用法: 獲得要獲得屬性的那個(gè)對(duì)象.getAttribute("屬性名(注意要加“”)")
var anchorTitle=anchor.getAttribute("title");
alert("The anchor ID is:"+anchorId+"\nThe anchor title is:"+anchorTitle);
』
『例子2』
『
<a id="antares" href="test2.htm" title="Marssion">Antares</a>
----------------------------------------------------------------
下面是外置JS腳本的內(nèi)容:
var anchor=document.getElementById("antares"); //獲得要獲得屬性的那個(gè)對(duì)象
anchor.setAttribute("title","Marssion.lee"); // 用法: 獲得要獲得屬性的那個(gè)對(duì)象.setAttrubute("屬性名(注意要加“”)","要給屬性名賦的值(注意要加“”)")
var newTitle=anchor.getAttribute("title"); //新的屬性值會(huì)覆蓋舊的屬性值
alert("The anchor title is:"+newTitle);
』
』
//獲得擁有特定屬性值的所有元素
『
如果需要在input元素中尋找滿足type=“checkbox”這個(gè)條件的所有元素
var inputs = document.getElementsByTagName("input");
for (var i = 0; i < inputs.length; i++)
{
if (inputs.getAttribute("type") == "checkbox")
{
...
}
}
解決方法——getElementsByAttribute函數(shù)
『例子』
『
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>無(wú)標(biāo)題頁(yè)</title>
<script language="javascript" type="text/javascript" src="javascript/getElementByAttribute.js">
</script>
</head>
<body>
<h1>Getting all element with a particular attribute value</h1>
<p id="starLinks">
<label for="test1">test1</label>
<a href="test1.htm" title="sirius">Sirius</a>
<a href="test2.htm" title="marssion">Marssion</a>
<a href="test2.htm" title="marssion">Marssion</a>
<a href="test2.htm" title="marssion">Marssion</a>
<a href="test2.htm" title="marssion">Marssion</a>
<a href="test2.htm" title="marssion">Marssion</a>
</p>
</body>
</html>
-------------------------------------------------------------
下面是外置JS腳本的內(nèi)容:
addLoadListener(init)
function init() {
var anchors = getElementsByAttribute("title", "marssion"); //取得屬性名為"title"屬性值為"marssion"的元素集合,此方法在下面定義了拿過(guò)來(lái)用就行了
alert("There are " + anchors.length + " elements with the title marssion");
var label = getElementsByAttribute("for", "test1");
alert(label.length);
}
function getElementsByAttribute(attribute,attributeValue) //這個(gè)函數(shù)是根據(jù)不同的瀏覽器判斷屬性和屬性值是否符合正則表達(dá)式
{
var elementArray=new Array();
var matchedArray=new Array();
if(document.all)
{
elementArray=document.all;//IE獲取所有的元素
}
else
{
elementArray=document.getElementsByTagName("*");//DOM獲取所有的元素
}
for(var i=0;i<elementArray.length;i++)
{
if(attribute=="title")
{
var pattern=new RegExp("(^| )"+attributeValue+"( |$)");//實(shí)例化正則表達(dá)式
if (pattern.test(elementArray[i].getAttribute(attribute)))//測(cè)試是否與表達(dá)式匹配
{
matchedArray[matchedArray.length]=elementArray[i];
}
}
else if(attribute=="for")
{
if(elementArray[i].getAttribute("htmlFor")||elementArray[i].getAttribute("for"))
{
if(elementArray[i].htmlFor==attributeValue)
{
matchedArray[matchedArray.length]=elementArray[i];
}
}
}
else if(elementArray[i].getAttribute(attribute)==attributeValue)
{
matchedArray[matchedArray.length]=elementArray[i];
}
}
return matchedArray;
}
function addLoadListener(fn) //這個(gè)函數(shù)是判斷瀏覽器的型號(hào)
{
if (typeof window.addEventListener != 'undefined')
{
window.addEventListener('load', fn, false);
}
else if (typeof document.addEventListener != 'undefined')
{
document.addEventListener('load', fn, false);
}
else if (typeof window.attachEvent != 'undefined')
{
window.attachEvent('onload', fn);
}
else
{
var oldfn = window.onload;
if (typeof window.onload != 'function')
{
window.onload = fn;
}
else
{
window.onload = function()
{
oldfn();
fn();
};
}
}
}
』
』
//元素的class的增減 //class css樣式表中的
『
元素的class都可以通過(guò)className屬性訪問(wèn)。
該屬性的值是字符串
『例子』
『
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>無(wú)標(biāo)題頁(yè)</title>
<link rel="Stylesheet" type="text/css" href="css/BlankNoses.css" />
<script language="javascript" type="text/javascript" src="javascript/AddRemoveClasses.js"></script>
</head>
<body>
<h1>Adding and removing multiple classes to/from an element</h1>
<p>
<a id="sirius" class="starLink" href="test1.htm">Sirius</a>
<a id="aldebaran" class="starLink" href="test2.htm">Aldebaran</a>
</p>
</body>
</html>
-------------------------------------------------------------------------------------------------------------
下面是外置JS腳本的內(nèi)容:
addLoadListener(init);
function init()
{
var sirius=document.getElementById("sirius");
var aldebaran=document.getElementById("aldebaran");
addClass(sirius, "important"); //函數(shù)在下面都有,拿過(guò)來(lái)用就行了 兩個(gè)函數(shù)都自動(dòng)驗(yàn)證正則表達(dá)式
removeClass(aldebaran,"starLink"); //函數(shù)在下面都有,拿過(guò)來(lái)用就行了
return true;
}
function addClass(target,classValue) {
//debugger;
var pattern = new RegExp("(^| )" + classValue + "( |$)");
if(!pattern.test(target.className))
{
if(target.className=="")
{
target.className=classValue;
}
else
{
target.className+=" "+classValue; //疊加樣式表用空格將兩個(gè)樣式表的名字分開(kāi)就行了
}
}
return true;
}
function removeClass(target,classValue)
{
var removedClass = target.className;
var pattern = new RegExp("(^| )" + classValue + "( |$)");
if(pattern.test(removedClass))
{
target.className="";
}
return true;
}
function addLoadListener(fn)
{
if (typeof window.addEventListener != 'undefined')
{
window.addEventListener('load', fn, false);
}
else if (typeof document.addEventListener != 'undefined')
{
document.addEventListener('load', fn, false);
}
else if (typeof window.attachEvent != 'undefined')
{
window.attachEvent('onload', fn);
}
else
{
var oldfn = window.onload;
if (typeof window.onload != 'function')
{
window.onload = fn;
}
else
{
window.onload = function()
{
oldfn();
fn();
};
}
}
}
』
』
//事件的概念
『
JavaScript程序使用的是事件驅(qū)動(dòng)的程序設(shè)計(jì)模型,某些元素發(fā)生了某些事件時(shí),Web瀏覽器就會(huì)生成一個(gè)事件(event)
同一個(gè)事件調(diào)用多個(gè)函數(shù)時(shí)用";"隔開(kāi)
』
//事件流
『
事件流意味著在頁(yè)面上可以有多個(gè)元素響應(yīng)同一個(gè)事件
分為兩類
『
1、冒泡型事件(IE) 從最先發(fā)生事件的地方一層一層向上返
2、DOM事件 從最上成(HTML)一層一層向下返
』
『
type
一個(gè)字符串,聲明發(fā)生的事件的類型,該屬性的值是刪除前綴“on”的事件處理程序名(如“click”或“mouseover”)
『例子』
『
var sType=oEvent.type;
function handleEvent(oEvent)
{
if(oEvent.type=="click")
{
alert("Cilcked!");
}
else if(oEvent.type=="mouseover")
{
alert("Mouse Over!");
}
oDiv.onclick=handleEvent;
oDiv.onmouseover=handleEvent;
}
』
』
復(fù)制代碼 代碼如下:
事件例子
復(fù)制代碼 代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script>
function alert1(fg) {
document.getElementById("display").innerHTML+=fg+"<br />"
}
function checkbtn() {
switch (event.button) {
case 1:
alert1("左鍵");
break;
case 2:
alert1("右鍵");
break;
case 3:
alert1("左鍵+右鍵");
break;
case 4:
alert1("中間鍵");
break;
case 5:
alert1("左鍵+中間鍵");
break;
case 6:
alert1("右鍵鍵+中間鍵");
break;
case 7:
alert1("左鍵+中間鍵+右鍵");
break;
}
}
</script>
</head>
<body>
<div onmousedown="checkbtn()" oncontextmenu="return false" style="width:50px;height:50px; background-color:Lime; cursor:hand;">單擊</div> //必須用 div 的onmousedown
<div id="display"></div>
</body>
</html>
』
復(fù)制代碼 代碼如下:
事件監(jiān)聽(tīng)
IE
[Object].attachEvent("on+eventName",fnHandler") //eventName :動(dòng)作的名字 fnHandler :事件函數(shù)
[Object].detachEvent("on+eventName",fnHandler")
DOM
[Object].addEventListener("eventName",fnHandler,bCapture)
[Object].removeEventListener("eventName",fnHandler,bCapture)
復(fù)制代碼 代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml" id="html1">
<head>
<title>無(wú)標(biāo)題頁(yè)</title>
<script language="javascript" type="text/javascript">
var fnClick=function()
{
alert("Clicked");
}
var fnClick1 = function() {
alert("body");
}
var fnClick2 = function() {
alert("html");
}
var fnLoad = function() {
var temp = document.getElementById("btn1");
var temp1 = document.getElementById("body1");
var temp2 = document.getElementById("html1");
temp1.addEventListener("click", fnClick1, true);
temp2.addEventListener("click", fnClick2, true);
temp.addEventListener("click", fnClick, true); //false用于冒泡階段 true用于捕獲階段
}
var fnDetachEvent=function()
{
var temp=document.getElementById("btn1");
temp.removeEventListener("click",fnClick,true);
}
</script>
</head>
<body onload="fnLoad()" id="body1">
<input type="button" id="btn1" value="click me" />
<input type="button" value="detachEvent" onclick="fnDetachEvent()" />
</body>
</html>
-----------------------------------------------------------------------
[code]例子2
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>無(wú)標(biāo)題頁(yè)</title>
<script language="javascript" type="text/javascript">
var fnClick=function()
{
alert("Clicked");
}
var fnLoad=function()
{
var temp=document.getElementById("btn1");
temp.attachEvent("onclick",fnClick);
}
var fnDetachEvent=function()
{
var temp=document.getElementById("btn1");
temp.detachEvent("onclick",fnClick);
}
</script>
</head>
<body onload="fnLoad()">
<input type="button" id="btn1" value="click me" />
<input type="button" value="detachEvent" onclick="fnDetachEvent()" />
</body>
</html>
[/code]
復(fù)制代碼 代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>無(wú)標(biāo)題頁(yè)</title>
<script language="javascript" type="text/javascript">
var EventUtil=new Object;
EventUtil.addEventHandler=function(oTarget,sEventType,fnHandler){
if(oTarget.addEventListener)
{
oTarget.addEventListener(sEventType,fnHandler,false);
}
else if(oTarget.attachEvent)
{
oTarget.attachEvent("on"+sEventType,fnHandler);
}
else //要是出現(xiàn)即不是IE又不是火狐的瀏覽器就執(zhí)行這條
{
oTarget["on"+sEventType]=fnHandler;
}
};
EventUtil.removeEventHandler=function(oTarget,sEventType,fnHandler){
if(oTarget.removeEventListener)
{
oTarget.removeEventListener(sEventType,fnHandler,false);
}
else if(oTarget.detachEvent)
{
oTarget.detachEvent("on"+sEventType,fnHandler);
}
else
{
oTarget["on"+sEventType]=null;
}
};
function handleClick()
{
alert("Click");
var oDiv=document.getElementById("div1");
EventUtil.removeEventHandler(oDiv,"click",handleClick);
}
window.onload=function(){
var oDiv=document.getElementById("div1");
EventUtil.addEventHandler(oDiv,"click",handleClick);
}
</script>
</head>
<body>
<div id="div1" style="background-color:Red; width:100px; height:100px;"></div>
</body>
</html>
//例子(補(bǔ)充)
復(fù)制代碼 代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script language="javascript" type="text/javascript">
function load() {
document.f1.b1.onmouseover = plead; //注意:document點(diǎn)出的是相應(yīng)的name屬性,等號(hào)后面調(diào)用的函數(shù)不用加()
}
function plead() {
window.status = "Please Press Me!";
}
</script>
</head>
<body onload="load()">
<form name="f1" action="">
<input name="b1" type="button" value="Press Me" /> //通過(guò)外置事件調(diào)用
</form>
</body>
</html>
復(fù)制代碼 代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>無(wú)標(biāo)題頁(yè)</title>
<script language="javascript" type="text/javascript">
function handleEvent(oEvent)
{
var oTextbox=document.getElementById("txt1");
oTextbox.value+="\n>"+oEvent.type; //把事件轉(zhuǎn)換成事件類型進(jìn)行輸出
}
</script>
</head>
<body>
<p>Type some characters into the first textbox.</p>
<p><textarea id="txtInput" rows="15" cols="50"
onkeydown="handleEvent(event)" onkeyup="handleEvent(event)" onkeypress="handleEvent(event)"></textarea></p> //注意函數(shù)傳值傳的是事件
<p><textarea id="txt1" rows="15" cols="50"></textarea></p>
</body>
</html>
復(fù)制代碼 代碼如下:
顯示調(diào)用事件
復(fù)制代碼 代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>無(wú)標(biāo)題頁(yè)</title>
<script language="javascript" type="text/javascript">
function oldHandle() {
alert("aa");
}
</script>
</head>
<body>
<form name="myForm" action="test1.htm" method="post">
<input name="myButton" type="button" value="Click Me" onclick="oldHandle()" />
</form>
<script language="javascript" type="text/javascript">
document.myForm.myButton.onclick();//顯示調(diào)用事件處理程序,但沒(méi)有引發(fā)onclick事件。
//alert(navigator.userAgent);
</script>
</body>
</html>
復(fù)制代碼 代碼如下:
關(guān)于事件的返回值
復(fù)制代碼 代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script language="javascript" type="text/javascript">
function check() {
var textbox = document.getElementById("txt");
if (textbox.value == "") {
alert("文本框空了");
textbox.focus();
return false;
}
else
{
return true;
}
}
</script>
</head>
<body>
<form id="form1" onsubmit="return check()"> <!--return 給事件一個(gè)boolean返回值,true代表能夠繼續(xù)執(zhí)行,F(xiàn)ALSE代表停止執(zhí)行(這里的check()相當(dāng)于一個(gè)boll值)-->
<input type="text" id="txt" /> //onsubmit是提交表單事件
<input type="submit" value="Submit" />
<!--<input type="button" onclick="sd()" />-->
</form>
</body>
</html>
-----------------------------------------------------------------------------
[code]例子2
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>無(wú)標(biāo)題頁(yè)</title>
<script language="javascript" type="text/javascript">
function confirmLink()
{
return confirm("Do you really want to visit "+this.href+" ?"); //這是一個(gè)有yesno的選項(xiàng) 返回的bool值指示函數(shù)是否繼續(xù)執(zhí)行
}
function confirmAllLinks()
{
for(var i=0;i<document.links.length;i++)
{
document.links[i].onclick=confirmLink; //動(dòng)態(tài)的將onclick事件進(jìn)行綁定,如果返回時(shí)true就執(zhí)行鏈接 返回false就不執(zhí)行鏈接
} //說(shuō)白了就是
復(fù)制代碼 代碼如下:
事件名=“boolean值(TRUE就執(zhí)行,F(xiàn)ALSE就不執(zhí)行)”
}
</script>
</head>
<body onload="confirmAllLinks()">
<a href="CreateElement1.htm" >點(diǎn)我看看</a>
<a href="CreateElement1.htm" >點(diǎn)我看看</a>
<a href="CreateElement1.htm" >點(diǎn)我看看</a>
<a href="CreateElement1.htm" >點(diǎn)我看看</a>
<a href="CreateElement1.htm" >點(diǎn)我看看</a>
<a href="CreateElement1.htm" >點(diǎn)我看看</a>
</body>
</html>
[/code]
復(fù)制代碼 代碼如下:
關(guān)于加載
復(fù)制代碼 代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>無(wú)標(biāo)題頁(yè)</title>
<script language="javascript" type="text/javascript">
// document.body.onload=function() //如果取消注釋則會(huì)出現(xiàn)錯(cuò)誤,因?yàn)榇a是一行一行運(yùn)行的加載到此處時(shí)Body還沒(méi)有加載,所以用body會(huì)出錯(cuò)
// { //此例子指示解釋一下,就算把body換成別的好像也不行,我是沒(méi)試出來(lái)行的
// alert("Loaded");
// }
</script>
</head>
<body>
This example won't work because it's assinging the <code>onload</code> event handler to the wrong place!
<script language="javascript" type="text/javascript">
document.body.onload=function() //可以正確執(zhí)行,因?yàn)閎ody已經(jīng)加載
{
alert("Loaded");
}
</script>
</body>
</html>
『
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>無(wú)標(biāo)題頁(yè)</title>
<script language="javascript" type="text/javascript">
function handleEvent(oEvent)
{
var oTextbox=document.getElementById("txt1");
oTextbox.value+="\n>"+oEvent.type; //見(jiàn)上面關(guān)于type的解釋
oTextbox.value+="\n target is "+(oEvent.target||oEvent.srcElement).id;
oTextbox.value+="\n keyCode is "+oEvent.keyCode; //這是整數(shù)屬性聲明keydown和keyup事件的鍵代碼以及keypress事件的unicode字符,用String.fromCharCode(oEvent.keyCode)方法可以把字符代碼轉(zhuǎn)換成字符串
oTextbox.value+="\n charCode is "+oEvent.charCode;
var arrKeys=[];
if(oEvent.shiftKey) //shiftKey ctrlkey altkey 這些bool值屬性聲明在鼠標(biāo)事件發(fā)生時(shí),是否按住了Alt、Ctrl、Shift鍵
{
arrKeys.push("Shift");
}
if(oEvent.ctrlKey)
{
arrKeys.push("Ctrl");
}
if(oEvent.altKey)
{
arrKeys.push("Alt");
}
oTextbox.value+="\n keys down are "+arrKeys+" Number is "+arrKeys.length;
}
</script>
</head>
<body>
<p>Type some characters into the first textbox.</p>
<p><textarea id="txtInput" rows="15" cols="50"
onkeydown="handleEvent(event)" onkeyup="handleEvent(event)" onkeypress="handleEvent(event)"></textarea></p>
<p><textarea id="txt1" rows="15" cols="50"></textarea></p>
</body>
</html>
』
『
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script language="javascript" type="text/javascript" src="javascript/detect.js"></script>
<script language="javascript" type="text/javascript">
function load() {
var textbox = document.getElementById("txt");
textbox.oncontextmenu = function(oEvent) { //textbox.oncontextmenu 這是控制右鍵的函數(shù),返回false就禁用右鍵了
//debugger;
if (isIE) { //瀏覽器要是ie的禁用方法 ,禁用文本框的右鍵
oEvent = window.event;
oEvent.returnValue = false;
}
else {
oEvent.preventDefault();
}
}
document.oncontextmenu = function(oEvent) { //禁用文本框外面的右鍵
if (isIE) {
oEvent = window.event;
oEvent.returnValue = false;
}
else {
oEvent.preventDefault();
}
}
}
</script>
</head>
<body onload="load()">
<input value="abcd" id="txt" />
</body>
</html>
-----------------------------------------------------------------------------------------------------
"javascript/detect.js"腳本中的內(nèi)容: 這是判斷具體的瀏覽器類型是什么,直接拿過(guò)來(lái)用就行了
var sUserAgent = navigator.userAgent;
var fAppVersion = parseFloat(navigator.appVersion);
function compareVersions(sVersion1, sVersion2) {
var aVersion1 = sVersion1.split(".");
var aVersion2 = sVersion2.split(".");
if (aVersion1.length > aVersion2.length) {
for (var i = 0; i < aVersion1.length - aVersion2.length; i++) {
aVersion2.push("0");
}
}
else if (aVersion1.length < aVersion2.length) {
for (var i = 0; i < aVersion2.length - aVersion1.length; i++) {
aVersion1.push("0");
}
}
for (var i = 0; i < aVersion1.length; i++) {
if (aVersion1[i] < aVersion2[i]) {
return -1;
}
else {
return 1;
}
}
return 0;
}
var isOpera = sUserAgent.indexOf("Opera") > -1;
var isMinOpera4 = isMinOpera5 = isMinOpera6 = isMinOpera7 = isMinOpera7_5 = false;
if (isOpera) {
var fOperaVersion;
if (navigator.appName == "Opera") {
fOperaVersion.fAppVersion;
}
else {
var reOperaVersion = new RegExp("Opera (\\d+\\.\\d+)");
reOperaVersion.test(sUserAgent);
fOperaVersion = parseFloat(RegExp["$1"]);
}
isMinOpera4 = fOperaVersion >= 4;
isMinOpera5 = fOperaVersion >= 5;
isMinOpera6 = fOperaVersion >= 6;
isMinOpera7 = fOperaVersion >= 7;
isMinOpera7_5 = fOperaVersion >= 7.5;
}
var isKHTML = sUserAgent.indexOf("KHTML") > -1 || sUserAgent.indexOf("Konqueror") > -1 || sUserAgent.indexOf("AppleWebKit") > -1;
var isMinSafari1 = isMinSafari1_2 = false;
var isMinKonq2_2 = isMinKonq3 = isMinKonq3_1 = isMinKonq3_2 = false;
if (isKHTML) {
var isSafari = sUserAgent.indexOf("AppleWebKit") > -1;
var isKonq = sUserAgent.indexOf("Konqueror") > -1;
if (isSafari) {
var reAppleWebKit = new RegExp("AppleWebKit\\/(\\d+(?:\\.\\d*)?)");
reAppleWebKit.test(sUserAgent);
var fAppleWebKitVersion = parseFloat(RegExp["$1"]);
isMinSafari1 = fAppleWebKitVersion >= 85;
isMinSafari1_2 = fAppleWebKitVersion >= 124;
}
else if (isKonq) {
var reKonq = new RegExp("Konqueror\\/(\\d+(?:\\.\\d+(?:\\.\\d)?)?)");
reKonq.test(sUserAgent);
isMinKonq2_2 = compareVersions(RegExp["$1"], "2.2") >= 0;
isMinKonq3 = compareVersions(RegExp["$1"], "3.0") >= 0;
isMinKonq3_1 = compareVersions(RegExp["$1"], "3.1") >= 0;
isMinKonq3_2 = compareVersions(RegExp["$1"], "3.2") >= 0;
}
}
var isIE = sUserAgent.indexOf("compatible") > -1 && sUserAgent.indexOf("MSIE") > -1 && !isOpera;
var isMinIE4 = isMinIE5 = isMinIE5_5 = isMinIE6 = isMinIE7 = false;
if (isIE) {
var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
reIE.test(sUserAgent);
var fIEVersion = parseFloat(RegExp["$1"]);
isMinIE4 = fIEVersion >= 4;
isMinIE5 = fIEVersion >= 5;
isMinIE5_5 = fIEVersion >= 5.5;
isMinIE6 = fIEVersion >= 6.0;
isMinIE7 = fIEVersion >= 7.0;
}
var isMoz = sUserAgent.indexOf("Gecko") > -1 && !isKHTML;
var isMinMoz1 = sMinMoz1_4 = isMinMoz1_5 = false;
if (isMoz) {
var reMoz = new RegExp("rv:(\\d+\\.\\d+(?:\\.\\d+)?)");
reMoz.test(sUserAgent);
isMinMoz1 = compareVersions(RegExp["$1"], "1.0") >= 0;
isMinMoz1_4 = compareVersions(RegExp["$1"], "1.4") >= 0;
isMinMoz1_5 = compareVersions(RegExp["$1"], "1.5") >= 0;
}
var isNS4 = !isIE && !isOpera && !isMoz && !isKHTML
&& (sUserAgent.indexOf("Mozilla") == 0)
&& (navigator.appName == "Netscape")
&& (fAppVersion >= 4.0 && fAppVersion < 5.0);
var isMinNS4 = isMinNS4_5 = isMinNS4_7 = isMinNS4_8 = false;
if (isNS4) {
isMinNS4 = true;
isMinNS4_5 = fAppVersion >= 4.5;
isMinNS4_7 = fAppVersion >= 4.7;
isMinNS4_8 = fAppVersion >= 4.8;
}
var isWin = (navigator.platform == "Win32") || (navigator.platform == "Windows");
var isMac = (navigator.platform == "Mac68K") || (navigator.platform == "MacPPC")
|| (navigator.platform == "Macintosh");
var isUnix = (navigator.platform == "X11") && !isWin && !isMac;
var isWin95 = isWin98 = isWinNT4 = isWin2K = isWinME = isWinXP = false;
var isMac68K = isMacPPC = false;
var isSunOS = isMinSunOS4 = isMinSunOS5 = isMinSunOS5_5 = false;
if (isWin) {
isWin95 = sUserAgent.indexOf("Win95") > -1
|| sUserAgent.indexOf("Windows 95") > -1;
isWin98 = sUserAgent.indexOf("Win98") > -1
|| sUserAgent.indexOf("Windows 98") > -1;
isWinME = sUserAgent.indexOf("Win 9x 4.90") > -1
|| sUserAgent.indexOf("Windows ME") > -1;
isWin2K = sUserAgent.indexOf("Windows NT 5.0") > -1
|| sUserAgent.indexOf("Windows 2000") > -1;
isWinXP = sUserAgent.indexOf("Windows NT 5.1") > -1
|| sUserAgent.indexOf("Windows XP") > -1;
isWinNT4 = sUserAgent.indexOf("WinNT") > -1
|| sUserAgent.indexOf("Windows NT") > -1
|| sUserAgent.indexOf("WinNT4.0") > -1
|| sUserAgent.indexOf("Windows NT 4.0") > -1
&& (!isWinME && !isWin2K && !isWinXP);
}
if (isMac) {
isMac68K = sUserAgent.indexOf("Mac_68000") > -1
|| sUserAgent.indexOf("68K") > -1;
isMacPPC = sUserAgent.indexOf("Mac_PowerPC") > -1
|| sUserAgent.indexOf("PPC") > -1;
}
if (isUnix) {
isSunOS = sUserAgent.indexOf("SunOS") > -1;
if (isSunOS) {
var reSunOS = new RegExp("SunOS (\\d+\\.\\d+(?:\\.\\d+)?)");
reSunOS.test(sUserAgent);
isMinSunOS4 = compareVersions(RegExp["$1"], "4.0") >= 0;
isMinSunOS5 = compareVersions(RegExp["$1"], "5.0") >= 0;
isMinSunOS5_5 = compareVersions(RegExp["$1"], "5.5") >= 0;
}
}
』
『
<html xmlns="http://www.w3.org/1999/xhtml" onclick="alert('html')">
<head>
<title>無(wú)標(biāo)題頁(yè)</title>
<script type="text/javascript" language="javascript" src="javascript/detect.js"></script> //src="javascript/detect.js" 判斷瀏覽器的類型,上面有~
<script type="text/javascript" language="javascript">
function handleClick(oEvent) //阻止冒泡
{
alert("input");
if(isIE) {
oEvent.cancelBubble=true; //ie的阻止冒泡的方法
}
else
{
oEvent.stopPropagation(); //其他瀏覽器的阻止冒泡的方法
}
}
</script>
</head>
<body onclick="alert('body')">
<input type="button" value="Click Me" onclick="handleClick(event)" /> //觸發(fā)onclick事件可彈出三個(gè)框,因?yàn)榘粹o包含在他們中間
</body>
</html>
』
JS 第六章 : 表單和表單元素 ↑↓←→
//獲取Form對(duì)象
復(fù)制代碼 代碼如下:
第一種方式:
document.forms[0]指的就是文檔中的第一個(gè)表單
第二種方式:
通過(guò)表單的名稱 document.forms[“formZ”];
第三種方式:
document.getElementByld(“forml”)
例子
復(fù)制代碼 代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript" language="javascript">
function myclick1() {
var form;
form = document.forms["samplename"]; //三種獲取Form對(duì)象的方法
// form = document.forms[0];
// form = document.getElementById("myForm");
alert(form.elements["mytext"].value); //輸出文本:Marssion //form.elements["mytext"].value 用于獲得表單下的某個(gè)元素的值 注意:[]中是用""
}
</script>
</head>
<body>
<form id="myForm" name="samplename" action="" method="post"> //method(提交表單的方法(指明瀏覽器是發(fā)送Get請(qǐng)求還是Post請(qǐng)求))[code] 1、Get :不安全,是將信息跟在URL后,最多255個(gè)字符
↑ 2、post :提交表單時(shí)用的方法,安全,放入的字符長(zhǎng)度不限
指向服務(wù)器的表單處理程序(一個(gè)URL地址,表單提交到的地址) 特殊用法:<!--action="javascript:alert('Submitted')"-->
<input type="text" value="Marssion" name="mytext"/>
↑
文本框中的默認(rèn)值
<input type="button" value="Click Me" onclick="myclick1()"/>
↑
按鈕上的文本
</form>
</body>
</html>
[/code]
//Form表單提交
復(fù)制代碼 代碼如下:
以前的方法
<input type="submit" value="提交按鈕提交"/>
采用javascript提交和重置
submit()和reset()方法
<input type="button" value="普通按鈕提交" onclick="document.forms[0].submit()"/>
//onsubmit和onreset事件
復(fù)制代碼 代碼如下:
Form對(duì)象還提供了事件處理程序onsubmit和onreset
如果返回false,就取消表單的提交和重置 注意:只有真正點(diǎn)擊Submit按鈕才會(huì)觸發(fā)onsubmit事件處理程序(給用“type="submit"”)
例子 onsubmit
復(fù)制代碼 代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>無(wú)標(biāo)題頁(yè)</title>
<script language="javascript" type="text/javascript">
function check() {
var txtName = document.getElementById("txtName");
var txtPwd = document.getElementById("txtPassword");
if (txtName.value == "") {
alert("請(qǐng)輸入用戶名");
txtName.focus();
return false;
}
if (txtPwd.value == "") {
alert("請(qǐng)輸入密碼");
txtPwd.focus();
return false;
}
return true;
}
</script>
</head>
<body>
<form id="form1" method="post" action="Handler.ashx" onsubmit="return check()"> <!-- 注意: 在 form中寫(xiě)入 onsubmit="return 函數(shù)名()"-->
<label for="txtName">Name:</label><br />
<input type="text" id="txtName" name="txtName" /><br />
<label for="txtPassword">Password:</label><br />
<input type="password" id="txtPassword" name="txtPassword" /><br />
<label for="selAge">Age:</label><br />
<select name="selAge" id="selAge">
<option>18-21</option>
<option>22-25</option>
<option>26-29</option>
<option>30-35</option>
<option>Over 35</option>
</select><br />
<label for="txtComments">Comments:</label><br />
<textarea rows="10" cols="50" id="txtComments" name="txtComments"></textarea><br />
<input type="submit" value="提交按鈕提交"/> <!--提交按鈕會(huì)觸發(fā)onsubmit事件-->
<input type="button" value="普通按鈕提交" onclick="document.forms[0].submit()" /> <!--普通按鈕可以成功提交,但是不能觸發(fā)onsubmit事件-->
</form>
<p>This example blocks the form submissiong(you won't see an alert box).</p>
</body>
</html>
例子onreset
復(fù)制代碼 代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>無(wú)標(biāo)題頁(yè)</title>
</head>
<body>
<form method="post" action="javascript:alert('Submitted')" onreset="return confirm('Really erase All data and start over?')">
<p>Type some values into the textboxes and then press reset.</p>
<label for="txtName">Name:</label><br />
<input type="text" id="txtName" name="txtName" /><br />
<label for="txtPassword">Password</label><br />
<input type="password" id="txtPassword" name="txtPassword" /><br />
<label for="selAge">Age:</label><br />
<select name="selAge" id="selAge">
<option>18-21</option>
<option>22-25</option>
<option>26-29</option>
<option>30-35</option>
<option>Over 35</option>
</select><br />
<label for="txtComments">Comments:</label><br />
<textarea rows="10" cols="50" id="txtComments" name="txtComments"></textarea><br />
<input type="submit" value="Submit Form" />
<input type="reset" value="重置按鈕" /> <!--和onsubmit不同不管是"type="reset"重置按鈕" 還是“onclick="document.forms[0].reset()"”都能成功調(diào)用onreset事件-->
<input type="button" value="普通按鈕" onclick="document.forms[0].reset()" />
</form>
</body>
</html>
//保證表單提交一次
復(fù)制代碼 代碼如下:
解決方法:使用一般按鈕,添加如下事件:
<input type=”button” value=”Submit”
onclick=”this.disabled=true; this.form.submit()” />
例子
復(fù)制代碼 代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>無(wú)標(biāo)題頁(yè)</title>
<script language="javascript" type="text/javascript">
function sub(){
setTimeout("sub1()",3000); <!--<script></script>中寫(xiě)的兩個(gè)函數(shù)是模擬網(wǎng)速慢的情況下延時(shí)提交的情況-->
}
function sub1(){
document.forms[0].submit();
}
</script>
</head>
<body>
<form method="post" action="../Default.aspx" name="form1">
<input type="text" name="txtName" value="" />
<input type="button" value="Submit Form" name="mySubmit1" onclick="mySubmit1.disabled=true;sub();" />
</form>
</body>
</html>
//表單和表單元素的name
復(fù)制代碼 代碼如下:
可以使用下面的表達(dá)式引用“address”的表單中的“zipcode”元素,表單和元素都要使用name屬性
document.address.zipcode
比使用硬編碼(位置依賴)的數(shù)組下標(biāo)要簡(jiǎn)潔得多:
document.forms[1].elements[4]
例子
復(fù)制代碼 代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script language="javascript" type="text/javascript">
function showTextBoxValue() {
//debugger
alert(document.forms[0].elements[1].value);
alert(document.myForm.txt.value); //打印出的是文本框中填入的值 注意!:document后面的都是元素的name!,而且不能點(diǎn)出只能硬打
}
</script>
</head>
<body>
<form id="form1" method="post" action="#" name="myForm">
<input type="text" id="txtName" name="txt" />
<input type="button" id="btnSubmit" value="ShowValue" onclick="showTextBoxValue()" />
</form>
</body>
</html>
//表單元素通用方法和屬性
復(fù)制代碼 代碼如下:
Type:一個(gè)只讀字符串,標(biāo)識(shí)表單元素的類型。
Form:對(duì)包含該元素的form對(duì)象的只讀引用 //就是表單A的一個(gè)元素B B.From=A
Name:由HTML的name性質(zhì)指定的只讀字符串。
Value:一個(gè)可讀寫(xiě)的字符串,指定了表單元素包含或表示的“值”。
方法:blur()和focus()
例子(blur)
復(fù)制代碼 代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>無(wú)標(biāo)題頁(yè)</title>
</head>
<body>
<p>Type a few characters in the textbox and then tab to the Submit button.
Then,click on the textbox and tab back to the Submit button without changing the text.
</p>
<form method="post" action="javascript:alert('Submited')"> <!--當(dāng)按下提交按鈕時(shí)發(fā)生的動(dòng)作-->
<input type="text" name="textbox" value="" onblur="alert('Blur')" onchange="alert('Change')" /> <!--失去焦點(diǎn)時(shí)發(fā)生onblur事件 文本框中的內(nèi)容發(fā)生改變當(dāng)失去焦點(diǎn)時(shí)發(fā)生onchange事件-->
<input type="submit" value="Submit Form" />
</form>
</body>
</html>
//常見(jiàn)的表單元素支持的事件處理程序
復(fù)制代碼 代碼如下:
Onclick:當(dāng)用戶在元素上點(diǎn)擊鼠標(biāo)時(shí)觸發(fā)。
Onchange:當(dāng)用戶改變了元素表示的值(通過(guò)輸入文本或選擇一個(gè)選項(xiàng))時(shí)觸發(fā)。
Onfocus:在表單元素收到輸入焦點(diǎn)是觸發(fā)
Onblur:在表單元素失去輸入焦點(diǎn)時(shí)觸發(fā)
//文本框?qū)ο?
復(fù)制代碼 代碼如下:
事件:
{
onBlur 文本框失去焦點(diǎn)
onChange 文本框的值被修改(事件將跟蹤用戶在文本框中所作的修改,當(dāng)用戶在文本框中完成修改之后,將激活該事件。)
onFocus 光標(biāo)進(jìn)入文本框中
}
方法:
{
select( ) 選中文本內(nèi)容,突出顯示輸入?yún)^(qū)域(選中文本內(nèi)容,突出顯示輸入?yún)^(qū)域,一般用于提示用戶重新輸入。(全部選中,就像QQ的輸入用戶名時(shí)一雙擊就將文本框中的內(nèi)容全部選中,一點(diǎn)刪除就全部刪除))
}
屬性:
{
value 表示用戶輸入的文本。
Size 規(guī)定文本可見(jiàn)長(zhǎng)度
Maxlength 規(guī)定可輸入最大長(zhǎng)度
readonly 某些文本框希望用戶不能修改,例如,某個(gè)拍賣網(wǎng)站的起拍價(jià),一般是賣方或拍賣公司定價(jià),所以希望用戶不能修改,這時(shí)可以指定readonly只讀屬性
}
例子(文本框?qū)ο?onChange事件)
復(fù)制代碼 代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>無(wú)標(biāo)題頁(yè)</title>
<script language="javascript" type="text/javascript">
function getLength()
{
var oTextbox1=document.getElementById("txt1");
var oTextbox2=document.getElementById("txt2");
alert("The length of txt1 is " + oTextbox1.value.length + "\n" //oTextbox1.value.length屬性是獲得文本框中文本的長(zhǎng)度
+"The length of txt2 is "+oTextbox2.value.length);
}
function change(){
document.getElementById("loading").src="img/loading_32x32.gif"; //指定圖片的路徑
document.getElementById("loading").style.display = "block"; //style.display="block" 含義是讓圖片顯示出來(lái)
}
function f(){
document.getElementById("loading").src="img/16checkcircle.gif"; //改變
}
</script>
</head>
<body>
<table cellpadding="0" cellspacing="0">
<tr>
<td><input type="text" size="12" id="txt1" onchange="change();" /></td> <!--當(dāng)用戶點(diǎn)擊別的地方,焦點(diǎn)離開(kāi)此文本框的時(shí)候,將觸發(fā)onchange事件-->
<td><img id="loading" width="17" height="17" style="display:none" alt="" /></td> <!--style="display:none" 含義是讓圖片隱藏-->
</tr>
</table>
<textarea rows="5" cols="25" id="txt2" onkeypress="f()"></textarea><br /> <!--onkeypress事件是當(dāng)用戶在多行文本框中鍵入值是觸發(fā)此事件-->
<input type="button" value="Get Length" onclick="getLength()" />
</body>
</html>
例子(文本框的文本屬性,(動(dòng)態(tài)添加子節(jié)點(diǎn)))
復(fù)制代碼 代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>無(wú)標(biāo)題頁(yè)</title>
<script language="javascript" type="text/javascript">
function getValues()
{
var oTextbox1=document.getElementById("txt1");
var oTextbox2=document.getElementById("txt2");
var resultDiv=document.getElementById("result");
var childNode=document.createTextNode(""); //建立一個(gè)空的文本節(jié)點(diǎn)
childNode.nodeValue=oTextbox1.value+oTextbox2.value;
if(resultDiv.childNodes.length==0) //如果層(父節(jié)點(diǎn))的子節(jié)點(diǎn)的個(gè)數(shù)等于0
{
resultDiv.appendChild(childNode);
}
else
{
resultDiv.removeChild(resultDiv.childNodes[0]); //如果層(父節(jié)點(diǎn))的子節(jié)點(diǎn)的個(gè)數(shù)不等于0 ,先刪除第一個(gè)子節(jié)點(diǎn),在加入新的子節(jié)點(diǎn)
resultDiv.appendChild(childNode);
}
}
</script>
</head>
<body>
<input type="text" size="12" id="txt1" /><br />
<textarea rows="5" cols="25" id="txt2"></textarea>
<input type="button" value="Get Values" onclick="getValues()" /><br />
<div id="result"></div>
</body>
</html>
例子(select方法)
復(fù)制代碼 代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>無(wú)標(biāo)題頁(yè)</title>
<script language="javascript" type="text/javascript">
function selectText()
{
var oTextbox1=document.getElementById("txt1");
oTextbox1.select(); //select()方法 :將相應(yīng)的文本框中的內(nèi)容全部選中
}
</script>
</head>
<body>
<input type="text" size="12" id="txt1" value="initial value" onselect="alert('Selected')" /><br /> <!--onselect事件:當(dāng)用戶選定文本時(shí)發(fā)生(不管選定多長(zhǎng))-->
<input type="button" value="Select Text" onclick="selectText()" />
</body>
</html>
//下拉列表框(List Boxes and Combo Boxes )
復(fù)制代碼 代碼如下:
屬性:
value 屬性表示該控件當(dāng)前選定的值。
size屬性表示可以顯示的項(xiàng)目數(shù)。
options集合
相關(guān)文章
詳解js中構(gòu)造流程圖的核心技術(shù)JsPlumb(2)
這篇文章主要介紹了js中構(gòu)造流程圖的核心技術(shù)JsPlumb,jsPlumb是一個(gè)強(qiáng)大的JavaScript連線庫(kù),它可以將html中的元素用箭頭、曲線、直線等連接起來(lái),適用于開(kāi)發(fā)Web上的圖表、建模工具等,需要的朋友可以參考下2015-12-12javascript九宮格圖片隨機(jī)打亂位置的實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了javascript九宮格圖片隨機(jī)打亂位置的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03關(guān)于JavaScript中的this指向問(wèn)題總結(jié)篇
在小編面試過(guò)程中經(jīng)常會(huì)遇到j(luò)avascript中this指向問(wèn)題,可以說(shuō)是前端面試必問(wèn),下面小編給大家總結(jié)了一下js中this的指向,感興趣的朋友一起學(xué)習(xí)吧2017-07-07學(xué)習(xí)使用Bootstrap頁(yè)面排版樣式
這篇文章主要教大家學(xué)習(xí)使用Bootstrap頁(yè)面排版樣式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05layui使用及簡(jiǎn)單的三級(jí)聯(lián)動(dòng)實(shí)現(xiàn)教程
這篇文章主要給大家介紹了關(guān)于layui使用及簡(jiǎn)單的三級(jí)聯(lián)動(dòng)的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12基于JavaScript實(shí)現(xiàn)一個(gè)簡(jiǎn)單的Vue
這篇文章主要介紹了基于JavaScript實(shí)現(xiàn)一個(gè)簡(jiǎn)單的Vue的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09