acara javascript menggelegak
Penulis:Eve Cole
Waktu Pembaruan:2009-06-11 16:22:17
Html代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transisi//EN"
" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns=" http://www.w3.org/1999/xhtml " lang="zh" xml:lang="zh">
<kepala>
<meta http-equiv="Jenis Konten" content="teks/html; charset=UTF-8" />
<meta name="pengembang" content="Realazy" />
<title>Gelembung di DOM JavaScript</title>
<style type="teks/css" media="layar">
div * {tampilan:blok; margin:4 piksel; bantalan:4 piksel; batas:1 piksel putih solid;}
area teks {lebar:20em; tinggi:2em;}
</gaya>
<skrip tipe="teks/javascript">
//<![CDATA[
fungsi init(){
var log = document.getElementsByTagName('textarea')[0];
var semua = document.getElementsByTagName('div')[0].getElementsByTagName('*');
for (var i = 0, n = semua.panjang; i < n; ++i){
semua[i].onmouseover = fungsi(e){
this.style.border = '1 piksel merah pekat';
log.value = '鼠标现在进入的是: ' + this.nodeName;
};
semua[i].onmouseout = fungsi(e){
this.style.border = '1 piksel putih solid';
};
}
var all2 = dokumen.getElementsByTagName('div')[1].getElementsByTagName('*');
for (var i = 0, n = semua2.panjang; i < n; ++i){
semua2[i].onmouseover = fungsi(e){
this.style.border = '1 piksel merah pekat';
if (e) //停止事件冒泡
e.stopPropagasi();
kalau tidak
window.event.cancelBubble = benar;
log.value = '鼠标现在进入的是: ' + this.nodeName;
};
semua2[i].onmouseout = fungsi(e){
this.style.border = '1 piksel putih solid';
};
}
}
jendela.onload = init;
//]]>
</skrip>
</kepala>
<tubuh>
<h1>Gelembung di DOM JavaScript</h1>
<p>DOM树的结构是:</p>
<pra><kode>
UL
- LI
- A
- rentang
</kode></pre>
<div>
<ul>
<li><a href="#"><span>Bubbllllllllllllll</span></a></li>
<li><a href="#"><span>Bubbllllllllllllll</span></a></li>
</ul>
</div>
<textarea></textarea>
<p>鼠标进入UL的任何一个子元素,如果不停止冒泡,我们从UL到SPAN都定义了鼠标悬停(<code>mouseover</code>)事UL, 从而从鼠标所进入的元素到UL元素都会有红色的边。</p>
<div>
<ul>
<li><a href="#"><span>Bubbllllllllllllll</span></a></li>
<li><a href="#"><span>Bubbllllllllllllll</span></a></li>
</ul>
</div>
<p>如果停止冒泡,事件不会上升,我们就可以获取精确的鼠标进入元素。</p>
</tubuh>
</html>