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)文章
JS實(shí)現(xiàn)拖動(dòng)模態(tài)框案例
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)拖動(dòng)模態(tài)框案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07關(guān)于js函數(shù)解釋(包括內(nèi)嵌,對(duì)象等)
下面小編就為大家?guī)?lái)一篇關(guān)于js函數(shù)解釋(包括內(nèi)嵌,對(duì)象等) 。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11typescript 類(lèi)型any不能分配給類(lèi)型never
本文主要介紹了typescript 類(lèi)型any不能分配給類(lèi)型never,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04JavaScript 數(shù)組運(yùn)用實(shí)現(xiàn)代碼
復(fù)習(xí)一下JS中數(shù)組的運(yùn)用。學(xué)習(xí)js數(shù)組的朋友可以參考下。2010-04-04基于Three.js實(shí)現(xiàn)酷炫3D地圖效果
這篇文章主要為大家詳細(xì)介紹了如何利用Three.js實(shí)現(xiàn)酷炫3D地圖的效果,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以嘗試一下2022-10-10微信公眾號(hào)開(kāi)發(fā)之微信支付代碼記錄的實(shí)現(xiàn)
這篇文章主要介紹了微信公眾號(hào)開(kāi)發(fā)之微信支付代碼記錄的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10