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>之間的內容。
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>之間的內容。
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學習小結之被嫌棄的eval函數(shù)和with語句實例詳解
- 深入淺析JavaScript中with語句的理解
- 理解javascript中的with關鍵字
- javaScript中with函數(shù)用法實例分析
- Javascript中With語句用法實例
- JavaScript嚴格模式禁用With語句的原因
- javascript中with()方法的語法格式及使用
- JavaScript的strict模式與with關鍵字介紹
- js批量設置樣式的三種方法不推薦使用with
- 關于JavaScript的with 語句的使用方法
- javascript下with 的簡化代碼寫法
- js的with語句使用方法
- 在js中使用"with"語句中跨frame的變量引用問題
- javascript之with的使用(阿里云、淘寶使用代碼分析)
相關文章
JavaScript 數(shù)組運用實現(xiàn)代碼
復習一下JS中數(shù)組的運用。學習js數(shù)組的朋友可以參考下。2010-04-04