javascript簡單事件處理和with用法介紹
更新時(shí)間:2013年09月16日 10:10:12 作者:
本文為大家介紹下javascript事件處理及with用法,主要是鼠標(biāo)點(diǎ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)擊后會彈出<div>之間的內(nèi)容。
2.鼠標(biāo)移動
<!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)">移動到這兒</div>
</body>
</html>
onmouseover鼠標(biāo)放到那兒時(shí),會把字體變大,字體成為紅色,離開時(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)擊后會彈出<div>之間的內(nèi)容。
2.鼠標(biāo)移動
復(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)">移動到這兒</div>
</body>
</html>
onmouseover鼠標(biāo)放到那兒時(shí),會把字體變大,字體成為紅色,離開時(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語句實(shí)例詳解
- 深入淺析JavaScript中with語句的理解
- 理解javascript中的with關(guān)鍵字
- javaScript中with函數(shù)用法實(shí)例分析
- Javascript中With語句用法實(shí)例
- JavaScript嚴(yán)格模式禁用With語句的原因
- javascript中with()方法的語法格式及使用
- JavaScript的strict模式與with關(guān)鍵字介紹
- js批量設(shè)置樣式的三種方法不推薦使用with
- 關(guān)于JavaScript的with 語句的使用方法
- javascript下with 的簡化代碼寫法
- js的with語句使用方法
- 在js中使用"with"語句中跨frame的變量引用問題
- javascript之with的使用(阿里云、淘寶使用代碼分析)
相關(guān)文章
關(guān)于js函數(shù)解釋(包括內(nèi)嵌,對象等)
下面小編就為大家?guī)硪黄P(guān)于js函數(shù)解釋(包括內(nèi)嵌,對象等) 。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11
JavaScript 數(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
微信公眾號開發(fā)之微信支付代碼記錄的實(shí)現(xiàn)
這篇文章主要介紹了微信公眾號開發(fā)之微信支付代碼記錄的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10

