javascript簡(jiǎn)單事件處理和with用法介紹
更新時(shí)間:2013年09月16日 10:10:12 作者:
本文為大家介紹下javascript事件處理及with用法,主要是鼠標(biāo)點(diǎn)擊與移動(dòng),感興趣的朋友可以參考下
這一期介紹一些簡(jiǎn)單的事件處理:
1.鼠標(biāo)點(diǎn)擊
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<script type="text/javascript">
function click1(obj)
{
alert(obj.innerHTML);
}
</script>
</head>
<body>
<div onclick="click1(this)" style="cursor:pointer">點(diǎn)擊我吧</div>
</body>
</html>
this即div的上下文,點(diǎn)擊后會(huì)彈出<div>之間的內(nèi)容。
2.鼠標(biāo)移動(dòng)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<script type="text/javascript">
function mouse1(obj)
{
obj.style.color = "#f00";
obj.style.fontSize = "18px"
}
function mouse2(obj)
{
obj.style.color = "#000";
obj.style.fontSize = "16px"
}
</script>
</head>
<body>
<div onmouseover="mouse1(this)" onmouseout="mouse2(this)">移動(dòng)到這兒</div>
</body>
</html>
onmouseover鼠標(biāo)放到那兒時(shí),會(huì)把字體變大,字體成為紅色,離開(kāi)時(shí)字體和顏色恢復(fù)。
3.with用法
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<script type="text/javascript">
with(document)
{
write("niujiabin"+"<br/>");
write("maybe"+"<br/>");
write("gossip"+"<br/>");
}
</script>
</head>
<body>
</body>
</html>
效果與如下相同
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<script type="text/javascript">
document.write("niujiabin"+"<br/>");
document.write("maybe"+"<br/>");
document.write("gossip"+"<br/>");
</script>
</head>
<body>
</body>
</html>
1.鼠標(biāo)點(diǎn)擊
復(fù)制代碼 代碼如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<script type="text/javascript">
function click1(obj)
{
alert(obj.innerHTML);
}
</script>
</head>
<body>
<div onclick="click1(this)" style="cursor:pointer">點(diǎn)擊我吧</div>
</body>
</html>
this即div的上下文,點(diǎn)擊后會(huì)彈出<div>之間的內(nèi)容。
2.鼠標(biāo)移動(dòng)
復(fù)制代碼 代碼如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<script type="text/javascript">
function mouse1(obj)
{
obj.style.color = "#f00";
obj.style.fontSize = "18px"
}
function mouse2(obj)
{
obj.style.color = "#000";
obj.style.fontSize = "16px"
}
</script>
</head>
<body>
<div onmouseover="mouse1(this)" onmouseout="mouse2(this)">移動(dòng)到這兒</div>
</body>
</html>
onmouseover鼠標(biāo)放到那兒時(shí),會(huì)把字體變大,字體成為紅色,離開(kāi)時(shí)字體和顏色恢復(fù)。
3.with用法
復(fù)制代碼 代碼如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<script type="text/javascript">
with(document)
{
write("niujiabin"+"<br/>");
write("maybe"+"<br/>");
write("gossip"+"<br/>");
}
</script>
</head>
<body>
</body>
</html>
效果與如下相同
復(fù)制代碼 代碼如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<script type="text/javascript">
document.write("niujiabin"+"<br/>");
document.write("maybe"+"<br/>");
document.write("gossip"+"<br/>");
</script>
</head>
<body>
</body>
</html>
您可能感興趣的文章:
- JavaScript學(xué)習(xí)小結(jié)之被嫌棄的eval函數(shù)和with語(yǔ)句實(shí)例詳解
- 深入淺析JavaScript中with語(yǔ)句的理解
- 理解javascript中的with關(guān)鍵字
- javaScript中with函數(shù)用法實(shí)例分析
- Javascript中With語(yǔ)句用法實(shí)例
- JavaScript嚴(yán)格模式禁用With語(yǔ)句的原因
- javascript中with()方法的語(yǔ)法格式及使用
- JavaScript的strict模式與with關(guān)鍵字介紹
- js批量設(shè)置樣式的三種方法不推薦使用with
- 關(guān)于JavaScript的with 語(yǔ)句的使用方法
- javascript下with 的簡(jiǎn)化代碼寫(xiě)法
- js的with語(yǔ)句使用方法
- 在js中使用"with"語(yǔ)句中跨frame的變量引用問(wèn)題
- javascript之with的使用(阿里云、淘寶使用代碼分析)
相關(guān)文章
用innerhtml提高頁(yè)面打開(kāi)速度的方法
這篇文章介紹了用innerhtml提高頁(yè)面打開(kāi)速度的方法,有需要的朋友可以參考一下2013-08-08
JS實(shí)現(xiàn)購(gòu)物車(chē)中商品總價(jià)計(jì)算
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)購(gòu)物車(chē)中商品總價(jià)的計(jì)算 ,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03
微信小程序?qū)崿F(xiàn)點(diǎn)擊圖片放大預(yù)覽
這篇文章主要為大家詳細(xì)介紹了小程序?qū)崿F(xiàn)點(diǎn)擊圖片放大預(yù)覽功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10
JS與HTML結(jié)合使用marquee標(biāo)簽實(shí)現(xiàn)無(wú)縫滾動(dòng)效果代碼
這篇文章主要介紹了JS與HTML結(jié)合使用marquee標(biāo)簽實(shí)現(xiàn)無(wú)縫滾動(dòng)效果代碼的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07
Javascript中Eval函數(shù)的使用說(shuō)明
JavaScript有許多小竅門(mén)來(lái)使編程更加容易。 其中之一就是eval()函數(shù),這個(gè)函數(shù)可以把一個(gè)字符串當(dāng)作一個(gè)JavaScript表達(dá)式一樣去執(zhí)行它。2008-10-10
使用flutter創(chuàng)建可移動(dòng)的stack小部件功能
本文主要介紹我為桌面和 Web 設(shè)計(jì)的一個(gè)超級(jí)秘密 Flutter 項(xiàng)目使用了畫(huà)布和可拖動(dòng)節(jié)點(diǎn)界面。本教程將展示我如何使用堆棧來(lái)使用小部件完成可拖動(dòng)功能,需要的朋友可以參考下2021-10-10
前端常用的4種請(qǐng)求方式實(shí)例總結(jié)
這篇文章詳細(xì)介紹了GET、POST、PUT和DELETE四種HTTP請(qǐng)求方法的使用方式、優(yōu)缺點(diǎn)及應(yīng)用場(chǎng)景,文中通過(guò)代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2025-01-01

