javascript簡單事件處理和with用法介紹
更新時間:2013年09月16日 10:10:12 作者:
本文為大家介紹下javascript事件處理及with用法,主要是鼠標點擊與移動,感興趣的朋友可以參考下
這一期介紹一些簡單的事件處理:
1.鼠標點擊
<!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">點擊我吧</div>
</body>
</html>
this即div的上下文,點擊后會彈出<div>之間的內(nèi)容。
2.鼠標移動
<!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鼠標放到那兒時,會把字體變大,字體成為紅色,離開時字體和顏色恢復。
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.鼠標點擊
復制代碼 代碼如下:
<!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">點擊我吧</div>
</body>
</html>
this即div的上下文,點擊后會彈出<div>之間的內(nèi)容。
2.鼠標移動
復制代碼 代碼如下:
<!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鼠標放到那兒時,會把字體變大,字體成為紅色,離開時字體和顏色恢復。
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>
您可能感興趣的文章:
- JavaScript學習小結(jié)之被嫌棄的eval函數(shù)和with語句實例詳解
- 深入淺析JavaScript中with語句的理解
- 理解javascript中的with關(guān)鍵字
- javaScript中with函數(shù)用法實例分析
- Javascript中With語句用法實例
- JavaScript嚴格模式禁用With語句的原因
- javascript中with()方法的語法格式及使用
- JavaScript的strict模式與with關(guān)鍵字介紹
- js批量設置樣式的三種方法不推薦使用with
- 關(guān)于JavaScript的with 語句的使用方法
- javascript下with 的簡化代碼寫法
- js的with語句使用方法
- 在js中使用"with"語句中跨frame的變量引用問題
- javascript之with的使用(阿里云、淘寶使用代碼分析)
相關(guān)文章
JS與HTML結(jié)合使用marquee標簽實現(xiàn)無縫滾動效果代碼
這篇文章主要介紹了JS與HTML結(jié)合使用marquee標簽實現(xiàn)無縫滾動效果代碼的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07
使用flutter創(chuàng)建可移動的stack小部件功能
本文主要介紹我為桌面和 Web 設計的一個超級秘密 Flutter 項目使用了畫布和可拖動節(jié)點界面。本教程將展示我如何使用堆棧來使用小部件完成可拖動功能,需要的朋友可以參考下2021-10-10

